繁体   English   中英

PHP print_r 不返回 json 请求

[英]PHP print_r not returning json request

以下代码运行没有任何错误,但它不返回任何内容。

如果我将它粘贴到我的浏览器中,它会返回我正在寻找的信息,我也可以用另一个 URL 替换它,它工作得很好。

$ch = curl_init();

$url = 'https://api.coronavirus.data.gov.uk/v1/data?filters=areaName=Somerset&structure={"date":"date","areaName":"areaName","areaCode":"areaCode","newCasesByPublishDate":"newCasesByPublishDate","cumCasesByPublishDate":"cumCasesByPublishDate","newDeathsByDeathDate":"newDeathsByDeathDate","cumDeathsByDeathDate":"cumDeathsByDeathDate"}';
    

curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$resp = curl_exec($ch);

$decode =json_decode($resp);
print_r($decode);


curl_close($ch);

您可以使用 postman 生成的以下代码

<?php
    
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => 'https://api.coronavirus.data.gov.uk/v1/',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'GET',
      CURLOPT_POSTFIELDS =>'{"date":"date","areaName":"areaName","areaCode":"areaCode","newCasesByPublishDate":"newCasesByPublishDate","cumCasesByPublishDate":"cumCasesByPublishDate","newDeathsByDeathDate":"newDeathsByDeathDate","cumDeathsByDeathDate":"cumDeathsByDeathDate"}',
      CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json'
      ),
    ));
    
    $response = curl_exec($curl);
    
    curl_close($curl);
    echo $response;

此代码有效。 原因是接收内容的编码。

try {
    $ch = curl_init();

    // Check if initialization had gone wrong*
    if ( $ch === false ) {
        throw new RuntimeException( 'failed to initialize' );
    }

    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL,
        'https://api.coronavirus.data.gov.uk/v1/data?filters=areaName=Somerset&structure={"date":"date","areaName":"areaName","areaCode":"areaCode","newCasesByPublishDate":"newCasesByPublishDate","cumCasesByPublishDate":"cumCasesByPublishDate","newDeathsByDeathDate":"newDeathsByDeathDate","cumDeathsByDeathDate":"cumDeathsByDeathDate"}' );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Solution is here. Response is the compressed content which your curl failed to detect. Empty encoding means to handle any type of encoding. It should solve your issue.
    curl_setopt($ch, CURLOPT_ENCODING, '');
    $content = curl_exec( $ch );
    curl_close( $ch );
    if ( $content === false ) {
        throw new RuntimeException( curl_error( $ch ), curl_errno( $ch ) );
    }

    /* Process $content here */


    $decode = json_decode( $content );

    var_dump( $decode );

    // Close curl handle
    curl_close( $ch );
} catch ( RuntimeException $e ) {

    trigger_error(
        sprintf(
            'Curl failed with error #%d: %s',
            $e->getCode(),
            $e->getMessage()
        ),
        E_USER_ERROR
    );

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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