0

file_get_contents returns an empty string on the url: http://thepiratebay.org/search/a

when it is obviosly not empty. also tried curl, heres my code

$ch = curl_init();
$cookieFile = 'cookies.txt';
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');

$url = 'http://thepiratebay.org/search/a';
curl_setopt($ch, CURLOPT_URL,$url);

$html = curl_exec ($ch);
var_dump($html);
$html = file_get_contents($url);
var_dump($html);

curl_close ($ch); unset($ch);

output is :

string(143) "HTTP/1.1 200 OK
X-Powered-By: PHP/5.3.8
Content-type: text/html
Content-Length: 0
Date: Mon, 14 Nov 2011 20:27:01 GMT
Server: lighttpd

"
string(0) ""

if i change the url to "http://thepiratebay.org/search" by deleting 2 chars everything is ok and i get a good response.

any ideas ?

2 Answers 2

2

The problem is that you're trying to set the user-agent string using CURLOPT_TIMEOUT. Try using CURLOPT_USERAGENT and that should solve your problem. You can do the same time using a stream_context_create or ini_set if you'd rather use file_get_contents.

Example for all three techniques are available at http://www.seopher.com/articles/how_to_change_your_php_user_agent_to_avoid_being_blocked_when_using_curl.

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

1 Comment

lolz, stupid mistake, ty, it was the CURLOPT_USERAGENT, i ended up writing this in python :)
0

file_get_contents may not be able to open URLs in your install. See fopen_wrappers. Or thepiratebay.org may be blocking you because of the user-agent on the file_get_contents call. Try passing a context. Or maybe it isn't returning anything because you aren't sending any cookies in your file_get_contents call like you are with curl. Bottom line, even though the URLs are the same, the requests are different.

Comments

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.