Curl in PHP having problems with redirects
Well, here's the code:
<?
// Getting the comic page
if (!empty($_GET['c'])) $id = $_GET['c'];
else $id = "new";
$url = "http://www.explosm.net/comics/$id/";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_TIMEOUT, 3);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.2 (KHTML, like Gecko) Chrome/6.0.447.0 Safari/534.2");
$result = curl_exec($curl);
curl_close($curl);
?>
(it's actually longer, but that's the part I have problem with)
So, the problem is: if I specify strip's ID in GET argument "c" (such as "2126"), everything works ok, and the strip's page gets loaded into $result var.
However, if I specify a term, such as "random" or "new" (both of these work fine on explosm website, you can check that if you want), $result just equals to nothing.
To 开发者_如何学JAVAtest that, you can use this page: http://expmo.1free.ws/test.php
( http://expmo.1free.ws/test.php?c=2126
works ok, but http://expmo.1free.ws/test.php
and http://expmo.1free.ws/test.php?c=random
doesn't)
Can you help me? Thank you.
What version of PHP are you using? If safe_mode
is enabled, curl will not follow redirects even when CURLOPT_FOLLOWLOCATION
is set. If you enable error reporting and see:
Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set...
Then you know you'll have to follow the redirects manually (since you're on shared hosting and likely cannot change the safe_mode
or open_basedir
settings) by parsing the response code and Location
header yourself.
精彩评论