can't get html source code with cUrl or file_get_contents

2.3k views Asked by At

sorry could not get the html source code of this url:http://www.picbear.com/tag/ok;

i always get a blank page with a ["Hello Stranger"] message.

no problems if i use chrome's tools.

i've always used file_get_contents or php_simple_html DOM parser with no problems.

i can't access to the php.ini because of my provider restrictions but it should be ok.

help anyone? thanks.

$url = "http://www.picbear.com/tag/ok";

$html = file_get_contents($url);

echo $html;
1

There are 1 answers

1
Georgy Ivanov On BEST ANSWER

Just add User-Agent header:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://picbear.com/tag/ok",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" // Here we add the header
  ),

));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}