How to use Http Client instead of cURL in CakePHP 3

I posted earlier “How to use HttpSocket instead of cURL in CakePHP 2“. In that blogpost I showed how easy it is to refactor your API requests from cURL to HttpSocket. In CakePHP3, HttpSocket is now Cake\Network\Http\Client and has been completely re-written from the ground up. CakePHP3 made cURL completely unnecessary with the new Http Client. Here are some examples to get you up to speed:

Basic requests

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
<?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);
/* ----------------------------------------------------------------
* -- CakePHP3 Http Client example
*/
use Cake\Network\Http\Client;
$http = new Client();
$response = $http->get('http://cakephp.org');
// Also try the full REST family:
$http->post('http://cakephp.org', ['key' => 'value']); // POST request
$http->put('http://cakephp.org', ['key' => 'value']); // PUT request
$http->patch('http://cakephp.org', ['key' => 'value']); // PATCH request
$http->delete('http://cakephp.org'); // DELETE request
?>

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
23
<?php
/* ----------------------------------------------------------------
* -- cURL example
*/
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://cakephp.org',
CURLOPT_USERPWD => 'myusername:mypassword'
]);
$output = curl_exec($ch);
curl_close($ch);
/* ----------------------------------------------------------------
* -- CakePHP3 Http Client example
*/
use Cake\Network\Http\Client;
$http = new Client();
$response = $http->get('http://cakephp.org', [], [
'auth' => ['username' => 'myusername', 'password' => 'mypassword']
]);
?>

Send and receive JSON

Let’s assume we send a JSON payload in the body request, and we receive JSON in the response

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
29
<?php
$jsonPayload = json_encode(['key' => 'value']);
/* ----------------------------------------------------------------
* -- cURL example
*/
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $jsonPayload,
CURLOPT_URL => 'http://cakephp.org',
CURLOPT_HTTPHEADER => ['Content-Type:application/json', 'Content-Length: ' . strlen($jsonPayload)]
]);
$response = curl_exec($ch);
curl_close($ch);
$jsonDecoded = json_decode($response, true); // Returns an array
debug($jsonDecoded);
/* ----------------------------------------------------------------
* -- CakePHP3 Http Client example
*/
use Cake\Network\Http\Client;
$http = new Client();
$response = $http->post('http://cakephp.org', $jsonPayload, ['type' => 'json']);
debug($response->json);
?>

For more examples please look at the Http Client CakePHP book.

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