简体   繁体   English

通过 CURL 发送 ajax 请求

[英]Send ajax request through CURL

An API request needs to be sent.需要发送 API 请求。 For some reason, the server is blocking CURL request, but it approves an XHR ajax request.出于某种原因,服务器阻止了 CURL 请求,但它批准了 XHR ajax 请求。 I could send an ajax request, but another problem arises - Mixed content my website is served over HTTPS but the request that needs to be sent is over HTTP so I cannot use ajax.我可以发送 ajax 请求,但出现了另一个问题 - 我的网站通过 HTTPS 提供混合内容,但需要发送的请求是通过 HTTP 发送的,因此我无法使用 ajax。

I am looking for a way to simulate ajax request through CURL, in some way, trick the server to believe that the CURL request is indeed an ajax request.我正在寻找一种通过 CURL 模拟 ajax 请求的方法,以某种方式欺骗服务器相信 CURL 请求确实是一个 ajax 请求。

Here's what I have tried.这是我尝试过的。

This is my CURL request.这是我的 CURL 请求。

    $ch = curl_init();
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64)');
        curl_setopt($ch, CURLOPT_REFERER, 'server's url');
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept:application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding:gzip, deflate',
        'Accept-Language:en-US,en;q=0.9',
        'Connection:keep-alive',
        'Content-Type: application/json; charset=utf-8',
        'X-Requested-With: XMLHttpRequest',
        '__RequestVerificationToken: $token'
        ));
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, base_path().'/cookies.txt');
        curl_setopt($ch, CURLOPT_COOKIEFILE, base_path().'/cookies.txt');
        $buffer = curl_exec($ch);
        if(curl_error($ch))
        {
        $buffer =   curl_error($ch);
        }
        curl_close($ch);

return $buffer;

This curl request is blocked此 curl 请求被阻止

But, this ajax request goes through my localhost, but since my live website uses HTTPS I cannot really use it.但是,这个 ajax 请求通过我的本地主机,但由于我的实时网站使用 HTTPS,我无法真正使用它。

     $.ajax({
          type: "get",
          xhrFields: { withCredentials:true },
          url: http://apiendpoint.com,
          success: function(data)
          {
           // console.log(data);
          }
    })

in chrome, you can copy a working curl expression from developer toolbar.在 chrome 中,您可以从开发人员工具栏中复制工作 curl 表达式。 Try with that one from cli.尝试使用 cli 中的那个。 If that works, you can figure out which parts are required and which parts are not.如果可行,您可以确定哪些部分是必需的,哪些部分不是。 Then you can transcript it to php.然后你可以将它转录到 php.ini 文件。

开发者工具栏 -> 网络 -> 选择文件 -> 右键单击​​ - 复制 -> 复制为 curl

If you have doubts if the same thing happens from php than from curl, just try it with requestbin .如果您怀疑 php 是否发生了与 curl 相同的事情,请尝试使用requestbin

I think it is possible the header for token may not be what you think it is, since given $a==1, '$a' converts to $a, but "$a" converts to "1" (notice single quotes vs double quotes).我认为令牌的标头可能不是您认为的那样,因为给定 $a==1,'$a' 转换为 $a,但 "$a" 转换为 "1"(注意单引号 vs双引号)。

in your example, try replacing:在您的示例中,尝试替换:

'__RequestVerificationToken: $token'

with:与:

"__RequestVerificationToken: $token"

and let us know if that solves the problem.让我们知道这是否能解决问题。

Consider using passthru("curl command here...");考虑使用 passthru("curl command here..."); using the suggestion from lintabá使用 lintabá 的建议

You can use 2-stage request您可以使用两阶段请求

1 curl .... -c ${cookie_file} 1 curl .... -c ${cookie_file}

2 curl .... -b ${cookie_file} -c ${cookie_file} 2 curl .... -b ${cookie_file} -c ${cookie_file}

This should work.这应该有效。 first is basicly gets the cookie with session id 2nd do the real request第一个基本上是获取带有会话 ID 的 cookie 第二个做真正的请求

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

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