0

I'm really confused. My problem is I can't get website content using curl I've tried to display the result but it always the same result, it always return an empty string..

Here is my function :

function get_html_content($url, $timeout=10) {
    // fake user agent
    $userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $string = curl_exec($ch);
    curl_close($ch);

    return $string;
}

Is there anything wrong with my code above, because whenever I try the above code, it always return an empty string.

Raj
  • 22,346
  • 14
  • 99
  • 142
  • I copied your code into a test file on my server, and it works. Are sure you are `echo`ing the returned result? – Joseph Silber Jan 02 '12 at 03:41
  • yes I do.. Here is my code where calling the above function $result = get_html_content('http://www.google.com'); echo $result; – Kira Yamato Jan 02 '12 at 04:04
  • oops sorry, I knew the problem already.. I forgot that the network I connected to is using proxy, and it seems that it's filtered by the proxy.. I've added CURLOPT_PROXY and insert my office proxy setting, and the code above work.. Thank you for your fast response :) – Kira Yamato Jan 02 '12 at 04:08
  • Hi Joseph, how to delete this question / mark this question as answered – Kira Yamato Jan 02 '12 at 04:14
  • 1
    @KiraYamato Please answer your own question and then mark that as `accepted`. – anubhava Jan 02 '12 at 05:01

1 Answers1

1

As mentioned in the comments, adding

CURLOPT_PROXY

with appropriate proxy settings solved the problem.

Raj
  • 22,346
  • 14
  • 99
  • 142