通过CURL发送ajax请求

Dhi*_*raj 13 php ajax jquery curl

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

我正在寻找一种通过CURL模拟ajax请求的方法,以某种方式欺骗服务器以相信CURL请求确实是一个ajax请求.

这是我尝试过的.

这是我的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);
Run Code Online (Sandbox Code Playgroud)

return $ buffer; 此卷曲请求被阻止

但是,这个ajax请求通过我的localhost,但由于我的实时网站使用HTTPS,我无法真正使用它.

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

lin*_*abá 8

在chrome中,您可以从开发人员工具栏复制工作卷曲表达式.尝试使用cli中的那个.如果可行,您可以确定哪些部件是必需的,哪些部件不是.然后你可以将它转录成php.

开发人员工具栏 - >网络 - >选择文件 - >右键单击 - 复制 - >复制为卷曲

如果你怀疑从php发生的事情是否与curl相同,那么只需使用requestbin进行尝试即可.