簡體   English   中英

通過GET方法發送http請求

[英]send http request through GET method

我正在嘗試通過 GET 方法訪問 API,但每次它都不返回任何內容。 這是我到目前為止嘗試過的代碼:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.printful.com');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'get');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
    
    $output = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    print_r($info);
    if ($info['http_code'] == 200) {
        print_r(json_decode ($output, true));
    } else {
           print_r(array("error" => array("message" => "ERROR: " . $info['http_code'])));
      }

當嘗試在瀏覽器中訪問 URL 時,它會帶來結果。

請求方法需要大寫。 如果您執行 GET 請求,您甚至可以將其丟棄,因為除非您不使用 post 數據,否則它是默認值。

刪除該行

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'get');

輸出將是

Array
(
    [code] => 200
    [result] => Welcome to the Printful API
    [extra] => Array
        (
        )

)

暫無
暫無

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

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