我试图从中检索数据的网页是用ajax调用特定页面来填充它的内容。相关的电话是这样的:
$.ajax({
type: 'POST',
dataType: "json",
data: Data,
url: '/example-ajax.aspx',
timeout: _defaultTimeOut,
...
这是HTTP请求(发布数据除外):
POST /example-ajax.aspx HTTP/1.1
Host: *website*
Connection: keep-alive
Content-Length: 83
Accept: application/json, text/javascript, */*; q=0.01
Origin: *http://website*
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: *referrer*
Accept-Encoding: gzip, deflate
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: *very long cokie*
我还可以使用Postman工具获取相关内容,只设置帖子值以及引荐来源,来源和主机标题。
我使用libcurl做的是首先使用登录信息连接到登录页面并将cookie保存在cookiejar中。比我向上述ajax页面发送请求,但它返回带有{Error:Unauthorized}参数的json数据。本地或网络存储中没有令牌或任何内容,只有cookie。那么服务器如何区分浏览器和cURL请求呢?
以下是我检索cookie的代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar);
//set the cookie the site has for certain features, this is optional
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
//curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://example.com");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host: example.com',
'Origin: http://example.com'
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo)
这是我的要求:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url;
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://example.com");
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host: example.com',
'Origin: http://example.com',
'X-Requested-With:XMLHttpRequest',
'Accept:application/json, text/javascript, */*; q=0.01',
'Accept-Language:tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4',
'Cache-Control:max-age=0',
'Connection:keep-alive',
'Content-Length:83',
'Content-Type:application/x-www-form-urlencoded; charset=UTF-8',
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
$result = curl_exec($ch);
curl_close($ch);
答案 0 :(得分:1)
在我发布问题之后,我发现只是发送GET请求而不是POST来解决问题。但由于某些原因Postman和Browser都发送POST请求来获取内容,我认为我也应该使用cURL。我错了。