Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过GET方法发送http请求_Php_Api_Http_Get - Fatal编程技术网

Php 通过GET方法发送http请求

Php 通过GET方法发送http请求,php,api,http,get,Php,Api,Http,Get,我试图通过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

我试图通过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
        (
        )

)

您可以删除
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'get')或使其
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'GET')使其工作。您最好删除它,因为默认情况下curl请求是GET请求。