Category: Utility Classes

Q: How to use curl requests in Cakephp 3?

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:

$this->Curl->get("http://www.foo.com");

Thanks:

SOLVED curl
1 Answers

Version: 3.0

user
Ksmacky

30

Accepted by Ksmacky

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

Replies: 1

Views: 928

Votes: 0

Welcome to Aero Coding!

Aero Coding is a CakePHP-focused Q&A community for professional and enthusiast cake bakers. It's built and run by you as part of the community.


Join Now Tour

Download Cakephp

Start baking your own CakePHP application!


Cakephp All Versions