[英]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.