簡體   English   中英

php-curl 請求返回被拒絕

[英]php-curl request returns denied

我目前正在嘗試通過此鏈接的 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);

關於可能是什么問題的任何想法?

如果您想在 CURL 中使用 SSL,您應該從以下位置獲取根證書: https : //curl.haxx.se/docs/caextract.html

只需使用內容頂部的鏈接下載 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\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);

直接訪問HTTPS URL:

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

沒有授權。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM