How to use HttpSocket instead of cURL in CakePHP 2

While browsing my Google Analytics, I got surprised how many people are still searching for CakePHP cURL solutions while CakePHP has it own build-in class (HttpSocket) with the very same functionality.

The main argument I hear for using cURL for your API requests, is because it has so many nice features. I like cURL and it helps me a lot, but I rather use what’s inside the CakePHP framework. HttpSocket has probably all the features you actually need and use. In this article I will show you how to refactor your app from cURL to CakePHP’s HttpSocket.

I will show some cURL vs HttpSocket examples for the following cases:

  • Basic GET request
  • Basic POST request
  • HttpSocket Request with basic HTTP authentication
  • HttpSocket Request with Cookies
  • HttpSocket Request that allows self-signed SSL connections
  • HttpSocket Request through a proxy

Basic GET request

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
/* ----------------------------------------------------------------
* -- cURL example
*/
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://cakephp.org'
]);
$response = curl_exec($ch);
curl_close($ch);
/* ----------------------------------------------------------------
* -- CakePHP HttpSocket example
*/
App::uses('HttpSocket', 'Network/Http');
$HttpSocket = new HttpSocket();
$response = $HttpSocket->get('http://cakephp.org');
?>

Basic POST request

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
/* ----------------------------------------------------------------
* -- cURL example
*/
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://cakephp.org',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => [
'key1' => 'value1',
'key2' => 'value2'
]
]);
$response = curl_exec($ch);
curl_close($ch);
/* ----------------------------------------------------------------
* -- CakePHP HttpSocket example
*/
App::uses('HttpSocket', 'Network/Http');
$HttpSocket = new HttpSocket();
$response = $HttpSocket->post('http://cakephp.org', ['key' => 'value']);
?>

HttpSocket Request with basic HTTP authentication

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
/* ----------------------------------------------------------------
* -- cURL example
*/
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://cakephp.org',
CURLOPT_USERPWD => 'username:password'
]);
$output = curl_exec($ch);
curl_close($ch);
/* ----------------------------------------------------------------
* -- CakePHP HttpSocket example
* You can also use "Digest" instead of "Basic" as the first parameter
*/
App::uses('HttpSocket', 'Network/Http');
$HttpSocket = new HttpSocket();
$HttpSocket->configAuth('Basic', 'username', 'password');
$response = $HttpSocket->get('http://cakephp.org');
?>

HttpSocket Request with Cookies

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
/* ----------------------------------------------------------------
* -- cURL example
*/
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://cakephp.org',
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEJAR => '/path/to/cookie.txt',
CURLOPT_COOKIEFILE => '/path/to/cookie.txt'
]);
$output = curl_exec($ch);
curl_close($ch);
/* ----------------------------------------------------------------
* -- CakePHP HttpSocket example
*/
App::uses('HttpSocket', 'Network/Http');
$HttpSocket = new HttpSocket();
$response = $HttpSocket->request([
'uri' => 'http://cakephp.org',
'cookies' => [
'CAKEPHP' => 'b6d3bca4d372dd6a58c4e46d2f60f0ef',
'cookieKey' => 'cookieValue'
]
]);
?>

HttpSocket Request that allows self-signed SSL connections

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
/* ----------------------------------------------------------------
* -- cURL example
*/
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://cakephp.org',
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false
]);
$output = curl_exec($ch);
curl_close($ch);
/* ----------------------------------------------------------------
* -- CakePHP HttpSocket example
* We have to supply an array when we initiate the HttpSocket:
*/
App::uses('HttpSocket', 'Network/Http');
$HttpSocket = new HttpSocket(['ssl_allow_self_signed' => true]);
$response = $HttpSocket->get('http://cakephp.org');
?>

HttpSocket Request through a proxy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
/* ----------------------------------------------------------------
* -- cURL example
*/
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://cakephp.org',
CURLOPT_PROXY => 'proxy-host-or-ip.com:3128',
CURLOPT_PROXYUSERPWD => 'proxyusername:proxypassword'
]);
$output = curl_exec($ch);
curl_close($ch);
/* ----------------------------------------------------------------
* -- CakePHP HttpSocket example
* configProxy attributes: configProxy($host, $port = 3128, $method = null, $user = null, $pass = null)
*/
App::uses('HttpSocket', 'Network/Http');
$HttpSocket = new HttpSocket();
$HttpSocket->configProxy('proxy-host-or-ip.com', 3128, 'Basic', 'proxyusername', 'proxypassword') {
$response = $HttpSocket->get('http://cakephp.org');
?>

Some other quick fixes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
/* User Agent */
$response = $HttpSocket->request([
'uri' => 'http://cakephp.org',
'header' => [
'User-Agent' => 'Internet Explorer 6 HAHA'
]
]);
/* PUT request */
$response = $HttpSocket->put('http://cakephp.org', ['key' => 'value']);
/* Alternative PUT/DELETE/PATCH/HEAD request */
$response = $HttpSocket->request([
'method' => 'HEAD',
'uri' => 'http://cakephp.org'
]);
/* Follow redirects */
$response = $HttpSocket->request([
'uri' => 'http://cakephp.org',
'redirect' => true // or an integer for max amount of redirects
]);
?>

So, what is your excuse to use cURL in your CakePHP project?