【问题标题】:php-curl request returns deniedphp-curl 请求返回被拒绝
【发布时间】:2019-08-11 08:48:35
【问题描述】:

我目前正在尝试使用来自此link 的 curl 请求获取一些数据。 这样做会返回以下内容:

“HTTP/2 403 服务器:AkamaiGHost mime-version: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 可以正常工作,因为它可以与其他请求一起使用,只是这个请求被拒绝了。 另外,使用浏览器打开链接不会显示“拒绝访问”错误,但实际上会返回我需要的数据。

这是从代码中复制粘贴的 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);

关于可能是什么问题的任何想法?

【问题讨论】:

标签: php php-curl


【解决方案1】:

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

只需下载带有内容顶部链接的 cacert.pm.. 并告诉连接到 SSL 时使用哪个证书。这设置了一个适当的连接(一个实际安全的连接,反对使用 ssl_verifyer 为假......)

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

我已经测试了以下代码可以使用的其他域,因此您必须与 g2a.com 的所有者交谈以处理此问题 (这是服务器问题,而不仅仅是代码问题) em>

<?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\stackoverflow\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);

【讨论】:

  • 嗨,cacert.pem 已经设置好了,因为我无法卷曲到另一个 https 域(它有效,所以我认为问题不在于 SSL 连接)。 CORS 是否仅适用于 curl 请求(在这种情况下)而不适用于用户制作的浏览器?谢谢!
  • CORS 适用于任何想要从其他服务器发出请求的人。如果您自己在浏览器中执行请求(直接在浏览器中输入链接),则不会应用 CORS。
【解决方案2】:

直接访问HTTPS URL:

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

没有授权。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多