0

I want to get any page of this web site: http://alhayat.com using curl.

This my code so far:

ini_set('display_errors',1);
error_reporting(E_ALL);
error_reporting(E_STRICT);
function getData($url){
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => 1, 
        CURLOPT_FOLLOWLOCATION => 1,
        CURLOPT_COOKIESESSION => true,
        CURLOPT_FILETIME => true,
        CURLOPT_CONNECTTIMEOUT => 55,
        CURLOPT_FAILONERROR => 0,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_TIMEOUT => 45,
        CURL_HTTP_VERSION => true,
        CURLOPT_HEADER => true,
        CURLOPT_BINARYTRANSFER => true,
    ));

    $data = curl_exec($ch);

    if(curl_errno($ch))
    {
        echo 'error:' . curl_error($ch);
    }

    curl_close($ch);

    $data = json_decode($data, true);

    return $data;
}
$url = 'http://alhayat.com';
var_dump(getData($url));

but the return value is always null.

I tried to find a solution, but nothing worked.

Any ideas?

7
  • 1
    What's in $data that is returned by this $data = curl_exec($ch); line? Why do you think it's json there? Commented Sep 6, 2014 at 1:35
  • what returns var_dump($data) ? Commented Sep 6, 2014 at 1:39
  • Before the Jason_decode function check the response by echo $data; to debug the response. Commented Sep 6, 2014 at 1:39
  • json_decode()? what are you expecting anyway? Commented Sep 6, 2014 at 1:48
  • Just by visiting the webpage (alhayat.com), there doesn't seem to be any JSON available to get. Commented Sep 6, 2014 at 2:07

1 Answer 1

1

Just delete the line

$data = json_decode($data, true);

that will return the site as return value

Sign up to request clarification or add additional context in comments.

3 Comments

i deleted this line ($data = json_decode($data, true);) and the script work in localhost, but in my server only this web site (alhayat.com) return (504 Gateway Time-out) and another web site work. whay?
maybe you have a slow connection on your server and need a longer CURLOPT_CONNECTTIMEOUT? Try set CURLOPT_CONNECTTIMEOUT => 600
i get this message: error: couldn't connect to hostbool(false). Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.