简体   繁体   English

带有XmlHttpRequest的PHP cURL

[英]PHP cURL with XmlHttpRequest

The scenario: There's this voting form (vote.php) with 3 fields where one is a hidden field containing a hash. 场景:有一个具有3个字段的投票表(vote.php),其中一个是包含哈希的隐藏字段。 Once you submit the form, it requests using GET to a separate script (process.php) via XHR. 提交表单后,它将通过XHR使用GET请求到单独的脚本(process.php)。 I am trying to simulate this via cURL but only gotten so far to getting the hash and preserving the phpsessionid using a cookie jar. 我正在尝试通过cURL进行模拟,但是到目前为止,还没有使用cookie罐子来获得哈希值和保留phpsessionid。

The problem: The processing script (process.php) seems to be able to detect if a request didn't push thru using XHR and will return an error if I just submit its required parameters using regular cURL GET. 问题:处理脚本(process.php)似乎能够检测到请求是否没有使用XHR进行推送,如果我只是使用常规cURL GET提交其必需的参数,则会返回错误。

So how do I simulate XHR in cURL? 那么,如何在cURL中模拟XHR? Or I may be even wrong in saying there's XHR in cURL so can you please advice any methods on how to achieve this. 或者说在cURL中存在XHR,我什至可能是错的,所以请您提供有关如何实现此目标的任何方法的建议。

There is a good chance that process.php checks for an XHR request by looking at the HTTP_X_REQUESTED_WITH header, for example: 很有可能process.php通过查看HTTP_X_REQUESTED_WITH标头来检查XHR请求,例如:

if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']=="XMLHttpRequest") { /* Do stuff here */ }

So you could try setting that header in your cUrl request: 因此,您可以尝试在您的cUrl请求中设置该标头:

curl -H "HTTP_X_REQUESTED_WITH:XMLHttpRequest"

That has a good chance of working. 那很有可能工作。 Good luck! 祝好运!

Add the following header: X-Requested-Width: XMLHttpRequest . 添加以下标头: X-Requested-Width: XMLHttpRequest This is added by all major JS libraries to ease identifying such requests on the server. 所有主要的JS库都添加了此功能,以便于在服务器上轻松识别此类请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM