亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

php-curl 請求返回被拒絕

php-curl 請求返回被拒絕

PHP
呼啦一陣風 2021-11-19 16:14:30
我目前正在嘗試通過此鏈接的 curl 請求獲取一些數據。這樣做會返回以下內容:“HTTP/2 403 服務器:AkamaiGHost mime-version:1.0 content-type:text/html content-length:293 expires:Sun,2019 年 8 月 11 日 08:34:24 GMT 日期:Sun,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);關于可能是什么問題的任何想法?
查看完整描述

2 回答

?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

如果您想在 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);


查看完整回答
反對 回復 2021-11-19
?
Helenr

TA貢獻1780條經驗 獲得超4個贊

直接訪問HTTPS URL:

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

沒有授權。


查看完整回答
反對 回復 2021-11-19
  • 2 回答
  • 0 關注
  • 296 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號