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.

5
  • I copied your code into a test file on my server, and it works. Are sure you are echoing the returned result? Commented Jan 2, 2012 at 3:41
  • yes I do.. Here is my code where calling the above function $result = get_html_content('google.com'); echo $result; Commented Jan 2, 2012 at 4: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 :) Commented Jan 2, 2012 at 4:08
  • Hi Joseph, how to delete this question / mark this question as answered Commented Jan 2, 2012 at 4:14
  • 1
    @KiraYamato Please answer your own question and then mark that as accepted. Commented Jan 2, 2012 at 5:01

1 Answer 1

1

As mentioned in the comments, adding

CURLOPT_PROXY 

with appropriate proxy settings solved the problem.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.