通过CURL发送Ajax请求 [英] Send ajax request through CURL

查看:134
本文介绍了通过CURL发送Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要发送API请求.由于某种原因,服务器阻止了CURL请求,但它批准了XHR ajax请求.我可以发送ajax请求,但是会出现另一个问题-我的网站通过HTTPS提供了混合内容,但是需要发送的请求是通过HTTP,因此我无法使用ajax.

An API request needs to be sent. For some reason, the server is blocking CURL request, but it approves an XHR ajax request. 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.

我正在寻找一种通过CURL模拟ajax请求的方法,以某种方式欺骗服务器以使CURL请求确实是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请求.

This is my CURL request.

    $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;

此卷曲请求已被阻止

但是,此ajax请求通过我的本地主机进行,但是由于我的实时网站使用HTTPS,所以我不能真正使用它.

But, this ajax request goes through my localhost, but since my live website uses HTTPS I cannot really use it.

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

在chrome中

推荐答案

,您可以从开发人员工具栏中复制有效的curl表达式.尝试使用cli中的那个.如果可行,则可以确定哪些部分是必需的,哪些部分不是必需的.然后,您可以将其转录为php.

in chrome, you can copy a working curl expression from developer toolbar. Try with that one from 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是否发生了相同的事情而不是curl发生了相同的事情,只需尝试使用 requestbin .

If you have doubts if the same thing happens from php than from curl, just try it with requestbin.

这篇关于通过CURL发送Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆