Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of file_get_contents() with CURL?

Tags:

php

curl

I'm trying to get some JSON data from a url like this:

$url = 'http://site.com/search.php?term=search term here';
$result = json_decode ( file_get_contents($url) );

However the client's webhost has got the allow_url_fopen setting disabled, hence the code above doesn't work.

What's the equivalent code of the lines above? Basically, a search term needs to be submitted via $_GETto the url.

like image 720
Ali Avatar asked Jul 06 '11 10:07

Ali


People also ask

Which is faster cURL or file_get_contents?

curl supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading HTTP form based upload, proxies, cookies. Curl is a much faster alternative to file_get_contents.

Does cURL work in PHP?

cURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains. Obtaining a copy of a website's material. Submission of forms automatically, authentication and cookie use.

What does cURL stand for PHP?

cURL stands for the client URL. PHP cURL is a library that is the most powerful extension of PHP. It allows the user to create the HTTP requests in PHP. cURL library is used to communicate with other servers with the help of a wide range of protocols.


1 Answers

Like this:

$url = 'http://site.com/search.php?term=search term here';

$rCURL = curl_init();

curl_setopt($rCURL, CURLOPT_URL, $url);
curl_setopt($rCURL, CURLOPT_HEADER, 0);
curl_setopt($rCURL, CURLOPT_RETURNTRANSFER, 1);

$aData = curl_exec($rCURL);

curl_close($rCURL);

$result = json_decode ( $aData );
like image 56
Jürgen Thelen Avatar answered Oct 13 '22 19:10

Jürgen Thelen