0

I'm trying to get content of a page through curl call. But I'm getting only an empty array. Here is my code

print_r(get_data('https://www.realestate.com.au/sold/in-9%2f20%3b/list-1'));
function get_data($url)
{
    $ch = curl_init();
    $timeout = 5;
    // the url to fetch
    curl_setopt($ch, CURLOPT_URL, $url);
    // return result as a string rather than direct output
     curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');
   curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($ch, CURLOPT_VERBOSE, 1);
    // set max time of cURL execution

   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
    }

I have also tried different method like file_get_contents function but always getting empty page.

  • Does this answer your question? [PHP cURL Not Working with HTTPS](https://stackoverflow.com/questions/9774349/php-curl-not-working-with-https) – bobble bubble Dec 14 '19 at 12:27

1 Answers1

0

given function is to complicated I think its a copy paste function, as I can see from return $result; } which is not needed and you should get syntax error.

probably that code will work without that bracket.

I cleared and shortened code, and giving a working and (tested) solution.

NOTE: You can add the code I removed back

$url 'your url';
//Usaqe of function I recomend to use parse_url();
echo get_data($url);
function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
  • Can you explain what you have changed and how this solves the problem. – Nigel Ren Dec 14 '19 at 11:55
  • I have just tried and same result. Showing blank page http://creativetech.design/demos/save/ – Umar Ayyaz Cheema Dec 14 '19 at 14:19
  • with normal link both code is working. When we try this link https://www.realestate.com.au/sold/in-9%2f20%3b/list-1 then its not working – Umar Ayyaz Cheema Dec 14 '19 at 14:20
  • What you mean with normal link ? you mean ssl ? if so add `curl_setopt($ch, CURLOPT_CAINFO, '/path/to/cert/file/cacert.pem');` into your code and download cacert file here https://curl.haxx.se/docs/caextract.htmlhttps://curl.haxx.se/docs/caextract.html if you meant url with spaces then its seo_url problem not the function. –  Dec 14 '19 at 14:30
  • sorry I add url twice in comment cacert file here `https://curl.haxx.se/docs/caextract.html` your site works with this url is well `https://www.realestate.com.au/sold/in-9/list-1` you need to remove that spaces in url. –  Dec 14 '19 at 14:47