Reputation: 78
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.
Upvotes: 0
Views: 471
Reputation: 9090
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
(
)
)
Upvotes: 1