php curl模拟ajax提交,使用Curl PHP模拟ajax调用

我正在使用curl(通过PHP)抓取一个网站,我想要的一些信息是产品列表,默认情况下仅显示前几个产品。其余的将在用户单击按钮以获取产品的完整列表时传递给用户,从而触发ajax调用以返回该列表。

简而言之,这是他们使用的JS:

headers['__RequestVerificationToken'] = token;

$.ajax({

type: "post",

url: "/ajax/getProductList",

dataType: 'html',

data: JSON.stringify({ historyPageIndex: 1, displayPeriod: 0, productsType: All }),

contentType: 'application/json; charset=utf-8',

success: function (result) {

$(target).html("");

$(target).html(result);

},

beforeSend: function (XMLHttpRequest) {

if (headers['__RequestVerificationToken']) {

XMLHttpRequest.setRequestHeader("__RequestVerificationToken", headers['__RequestVerificationToken']);

}

}

});

这是我的PHP脚本:

curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_MAXREDIRS, 10);

curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieLocation);

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieLocation);

curl_setopt($ch, CURLOPT_POST, false);

curl_setopt($ch, CURLOPT_URL, 'https://www.domain.com/Applications/ViewProducts');

curl_setopt($ch, CURLOPT_REFERER, 'https://www.domain.com/');

$webpage = curl_exec($ch);

$productsType = trim(find_by_pattren($webpage, '

$token = trim(find_by_pattren($webpage, '

$postVariables = 'productsType='.$productsType.

'&historyPageIndex=1

&displayPeriod=0

&__RequestVerificationToken='.$token;

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, $postVariables);

curl_setopt($ch, CURLOPT_URL, 'https://www.domain.com/ajax/getProductList');

curl_setopt($ch, CURLOPT_REFERER, 'https://www.domain.com/Applications/ViewProducts');

$webpage = curl_exec($ch);

这将在该站点上产生一个错误页面。我认为主要原因可能是:

他们检查这是否是ajax请求(不知道如何解决)

令牌必须在标题中,而不在post变量中

任何想法?

编辑:这是工作代码:

curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_MAXREDIRS, 10);

curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieLocation);

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieLocation);

curl_setopt($ch, CURLOPT_URL, 'https://www.domain.com/Applications/ViewProducts');

curl_setopt($ch, CURLOPT_REFERER, 'https://www.domain.com/');

$webpage = curl_exec($ch);

$productsType = trim(find_by_pattren($webpage, '

$token = trim(find_by_pattren($webpage, '

$postVariables = json_encode(array('productsType' => $productsType,

'historyPageIndex' => 1,

'displayPeriod' => 0));

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-With: XMLHttpRequest", "Content-Type: application/json; charset=utf-8", "__RequestVerificationToken: $token"));

curl_setopt($ch, CURLOPT_POSTFIELDS, $postVariables);

curl_setopt($ch, CURLOPT_URL, 'https://www.domain.com/ajax/getProductList');

curl_setopt($ch, CURLOPT_REFERER, 'https://www.domain.com/Applications/ViewProducts');

$webpage = curl_exec($ch);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值