How to Use Curl Instead of File_Get_Contents

How to use CURL instead of file_get_contents?

try this:

function file_get_contents_curl($url) {
$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}

How to convert file_get_contents to cURL

You may still get a 401 with curl but you can try the following

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://2strok.com/download/download.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, json_decode($output)->data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

header("Location: ".json_decode($output)->data);

You can reuse the curl handle if you'd like

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://2strok.com/download/download.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, json_decode($output)->data);
$output = curl_exec($ch);
curl_close($ch);

header("Location: ".json_decode($output)->data)

File_get_contents to Curl

Here are the simplest way to perform a get request:

<?php 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.someexample.com/api/findusers/".$names."?key=".$key);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$userID = curl_exec($ch);
curl_close($ch);
?>

CURL alternative to the built-in file_get_contents() function

If you have a problem with your script you need to debug it. For example:

$data = curl_exec($ch);
var_dump($data); die();

Then you will get an output what $data is. Depending on the output you can further decide where to look next for the cause of the malfunction.

Equivalent of file_get_contents() with CURL?

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 );


Related Topics



Leave a reply



Submit