怎样才能找到使用卷发将被重定向的位置?我正试着让卷发跟随重定向,但我不能完全让它正常工作。我有一个字符串,我想把它作为一个get param发送到一个服务器,并得到结果URL。例子:字符串=科博德害虫URL=www.wowarad.com/Search?Q=Kobold+Worker如果您访问该url,它将您重定向到“www.wowarad.com/npc=257”。我希望curl将这个URL返回到我的PHP代码中,这样我就可以提取“npc=257”并使用它。当前代码:function npcID($name) {
$urltopost = "http://www.wowhead.com/search?q=" . $name;
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
curl_setopt($ch, CURLOPT_URL, $urltopost);
curl_setopt($ch, CURLOPT_REFERER, "http://www.wowhead.com");
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type:application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
return curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);}然而,这将返回www.wowarad.com/Search?Q=Kobold+Worker而不是www.wowarad.com/npc=257.我怀疑PHP是在外部重定向发生之前返回的。我怎么才能解决这个问题?
3 回答
慕斯王
TA贡献1864条经验 获得超2个赞
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_getinfo()
$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_HEADER, true);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);$result = curl_exec($ch);if (preg_match('~Location: (.*)~i', $result, $match)) { $location = trim($match[1]);}
一只甜甜圈
TA贡献1836条经验 获得超5个赞
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$redirectURL = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL );
$ch = curl_init($url);curl_setopt($ch, CURLOPT_HEADER, false);curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); curl_setopt($ch, CURLOPT_TIMEOUT, 60);$html = curl_exec($ch); $redirectURL = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL );curl_close($ch);
- 3 回答
- 0 关注
- 369 浏览
添加回答
举报
0/150
提交
取消