我正在尝试使curl遵循重定向,但我不能让它正常工作.我有一个字符串,我想作为GET参数发送到服务器并获取结果URL.
例:
String = Kobold Vermin
Url = www.wowhead.com/search?q=Kobold+Worker
如果您转到该网址,它会将您重定向到"www.wowhead.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);
}
Run Code Online (Sandbox Code Playgroud)
然而,这将返回www.wowhead.com/search?q=Kobold+Worker而不是www.wowhead.com/npc=257.
我怀疑在外部重定向发生之前PHP正在返回.我怎样才能解决这个问题?
我正在尝试使用jquery的ajax函数来从我的ajax.php文件中获取一些信息.
码:
function ajaxIt(dataLine){
$.ajax({
type: "POST",
url: "ajax.php",
data: "ajax=true&"+dataLine,
success: function(msg){
console.log("[AjaxIt]: "+dataLine+" returned "+msg);
return msg;
}
});
}
if(ajaxIt("action=loggedIn")=="1"){
console.log("Logged In");
loggedIn=true;
initiate2();
}
Run Code Online (Sandbox Code Playgroud)
问题是我无法获得成功函数一直返回到ajaxIt函数.谁能解释一下我怎么做那样的事情?
谢谢.