PHP:使用cURL模拟XHR [英] PHP: Simulate XHR using cURL
问题描述
今天,我正试图为某些论坛登录做一份Cron工作,以检查在线统计信息. login.php脚本以提交值的形式接受ajax请求:用户,密码,服务器和哈希ID(在隐藏字段中).我已经可以将值提交到登录脚本,还可以使用Cookie罐保存会话,但是当我尝试传递所需的参数(来自sendlogin.php)时,它似乎拒绝了没有正确请求标头的请求.因此,我需要知道如何使用cURL进行模拟:
Today I'm trying to make a cron job for some forum login to check for online stats. The login.php script accepts an ajax request with the form submitted values: user, password, server, and a hash id (in a hidden field). I can already submit the values to the login script and also preserve the session using a cookie jar but when I try to pass the required parameters (coming from sendlogin.php), it seems to reject requests that do not come with the proper request headers. So I need to know how I could simulate this using cURL:
GET login.php?user=foo&password=bar&server=1&id=7131c359e534e3790eaefa495d621b2a HTTP/1.1
Host: someloginserver.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
X-Requested-With: XMLHttpRequest
Referer: http://someloginserver.com/sendlogin.php
Cookie: __cfduid=de2fe207593d1135fc2426fb06e3270741303115848; hellobar_current=1300711502; hellobar_1300711502_variation=11462; PHPSESSID=cc621c6f57c43130d51d3147e319d8c2
希望您能在此方面为我提供帮助.
hope you could help me on this.
推荐答案
在要卷曲的PHP api中,您可以使用:
In the PHP api to curl you can use:
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Host" => "someloginserver.com",
"User-Agent" => "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Accept" => "application/json, text/javascript, */*; q=0.01",
"Accept-Language" => "en-us,en;q=0.5",
"Accept-Encoding" => "gzip, deflate",
"Accept-Charset" => "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
"Keep-Alive" => "115",
"Connection" => "keep-alive",
"X-Requested-With" => "XMLHttpRequest",
"Referer" => "http://someloginserver.com/sendlogin.php"
));
但是您实际的问题可能是Cookie :,我在上面已排除了它.使用COOKIEJAR设置您的cURL请求.发出一个人造请求以获取当前会话值,然后才发送您的实际XHR请求.
But your actual problem might be the Cookie:, which I've excluded above. Setup your cURL request with a COOKIEJAR. Make one faux request to get a current session value, and only afterwards send your actual XHR request.
这篇关于PHP:使用cURL模拟XHR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!