PHP使用curl进行会自动跳转的POST提交

使用curl进行post提交,提交时会进行autoRedirect那么需要附上参数

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);


以下为详细案例
Example:

String = Kobold Vermin
Url = www.wowhead.com/search?q=Kobold+Worker

If you go to that url it will redirect you to “www.wowhead.com/npc=257”. I want curl to return this URL to my PHP code so that i can extract the “npc=257” and use it.

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);
        curl_exec(ch);
    return curl_getinfo(ch, CURLINFO_EFFECTIVE_URL);
}

附注:
在linux中可以使用curl -L 进行操作

0条留言