Kevin Kevin - 2 months ago 9x
HTTP Question

PHP Cannot get the text from an URL (file_get_content, CURL did not work)

I am trying to get the text from the following url.

I have tried several methods but none of them worked. I am really desperate right now please help me and thanks in advance.

P.S. I have enabled allow_url_fopen in my ini.php already.
P.S. I am using XAMPP v3.3.2 and PHP v5.6.23

the following are the codes that I have tried (and failed)


$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
echo $result;

result: Empty string


echo file_get_contents($url, true);

result: failed to open stream: HTTP request failed!


echo readfile($url)

result: output the address itself


Hi you have to pass header in your curl request, so server can treat its as browser request.


 $requestHeaders = array(
    "Accept:application/json, text/javascript, */*; q=0.01",
    "User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $output = curl_exec($ch);
 echo $output;