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
Php.Ini Is Nonexistent Loaded Configuration File (None)
Access PHP Variable in JavaScript
Send Cookie with File_Get_Contents
Find Common Values in Multiple Arrays with PHP
PHP Add Element to Every Sub Array of Multi Dimension Array
How to Install Composer on a Shared Hosting
Cakephp 2.0 - How to Make Custom Error Pages
How to Get Ssl Certificate Info with Curl in PHP
Add Extra Meta for Orders in Woocommerce
Recursively Remove Empty Elements and Subarrays from a Multi-Dimensional Array
Python Equivalent of PHP's Compact() and Extract()
PHP - Split String in Key/Value Pairs
How to Read Cookie/Session Value While Executing PHP5 Script Through Command Prompt
Get the Query Executed in Laravel 3/4
How to Get Client Ip Address in Laravel 5+
PHP MySQL Search Multiple Tables Using a Keyword