php-curl 请求 returns 被拒绝

php-curl request returns denied

我目前正在尝试通过此 link 的 curl 请求获取一些数据。 这样做 returns 以下内容:

"HTTP/2 403 服务器:AkamaiGHost mime 版本:1.0 内容类型:text/html 内容长度:293 过期时间:2019 年 8 月 11 日,星期日 08:34:24 GMT 日期:星期日, 2019 年 8 月 11 日 08:34:24 格林威治标准时间

拒绝访问

您没有权限访问“http://www.g2a.com/lucene/search/filter?”在此服务器上。

参考 #18.9d0c1502.1565512464.22e1446"

我知道 curl 可以正常工作,因为它可以处理其他请求,只是这个请求被拒绝了。 此外,用浏览器打开 link 并没有显示 "Access Denied" 错误,而是 returns 我需要的数据。

这是从代码中复制粘贴的 curl 请求:

try{
    //  initiate curl (used to request data from other webpages)
    $ch = curl_init();
    // will return the response, if false it prints the response
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // set the url, eliminates headers from response
    curl_setopt($ch, CURLOPT_URL, $g2a);
    curl_setopt($ch, CURLOPT_HEADER, true); 
    // execute
    $result=curl_exec($ch);

    //if some error occurs
    if (!$result)
        throw new Exception(curl_error($ch), curl_errno($ch));

    // Closing
    curl_close($ch);
} catch(Exception $e) {
    trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()), E_USER_ERROR);
}
var_dump($result);
//converts json to associative array
$result=json_decode($result, true);

对可能出现的问题有什么想法吗?

直接访问 HTTPS URL:

$g2a = "https://www.g2a.com/lucene/search/filter";

没有授权。

如果您想将 SSL 与 CURL 一起使用,您应该从以下位置下载根证书:https://curl.haxx.se/docs/caextract.html

只需下载内容顶部带有 link 的 cacert.pm.. 并告知连接到 SSL 时使用哪个证书。这设置了一个适当的连接(一个实际安全的连接,而不是使用 ssl_verifyer 到 false...)

我的合格猜测是您要连接的服务器可能没有将任何传入请求设置为有效(通过称为 CORS(访问控制允许来源)的东西)。如果您想从 www.yourdomain.com 连接,那么他们必须设置 www.yourdomain.com 对传入请求有效。

我已经测试了以下代码可以使用的其他域,因此您必须与 g2a.com 的所有者联系才能处理此问题 (这是一个服务器问题,不仅代码问题)

<?php
$g2a = 'https://www.g2a.com';

//Tell cURL where our certificate bundle is located.

//an absolute path to your downloaded pem-file:
$certificate = "C:\wamp\www\Whosebug\cacert.pem"; 

try{
    //  initiate curl (used to request data from other webpages)
    $ch = curl_init();
    // will return the response, if false it prints the response
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CAINFO, $certificate);
    curl_setopt($ch, CURLOPT_CAPATH, $certificate);

    // set the url, eliminates headers from response
    curl_setopt($ch, CURLOPT_URL, ($g2a) );
    curl_setopt($ch, CURLOPT_HEADER, true); 
    // execute
    $result=curl_exec($ch);

    //if some error occurs
    if (!$result)
        throw new Exception(curl_error($ch), curl_errno($ch));

    // Closing
    curl_close($ch);
    } catch(Exception $e) {
        trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e- 
        >getMessage()), E_USER_ERROR);
}
var_dump($result);

//converts json to associative array
$result=json_decode($result, true);