Category: Utility Classes
Question is how to use curl requests in Cake 3?
$ch = curl_init("http://www.foo.com/"); $fp = fopen("example_homepage.txt", "w"); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp);
I'd like to know if there is a better way to do so like:
Try Cake's Http Client:
CakePHP includes a basic but powerful HTTP client which can be used for making requests. It is a great way to communicate with webservices, and remote APIs.
e.g. from the cake book
use Cake\Network\Http\Client; $http = new Client(); // Simple get $response = $http->get('http://example.com/test.html'); // Simple get with querystring $response = $http->get('http://example.com/search', ['q' => 'widget']); // Simple get with querystring & additional headers $response = $http->get('http://example.com/search', ['q' => 'widget'], [ 'headers' => ['X-Requested-With' => 'XMLHttpRequest'] ]);
Created: 25 Sep '16
Last Reply: 25 Sep '16
Start baking your own CakePHP application!