简体   繁体   中英

send http request through GET method

I'm trying to access an API through GET method but each time it returns nothing. 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.

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.

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
        (
        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM