简体   繁体   English

通过GET方法发送http请求

[英]send http request through GET method

I'm trying to access an API through GET method but each time it returns nothing.我正在尝试通过 GET 方法访问 API,但每次它都不返回任何内容。 Here is the code I tried so far:这是我到目前为止尝试过的代码:

    $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'])));
      }

when trying to URL in the browser it will bring the result.当尝试在浏览器中访问 URL 时,它会带来结果。

Request methods need to be uppercase.请求方法需要大写。 If you do a GET request, you can even let it away, because it is the default unless you are not using post data.如果您执行 GET 请求,您甚至可以将其丢弃,因为除非您不使用 post 数据,否则它是默认值。

Remove the line删除该行

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

And the output will be输出将是

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