doutan1905
2019-04-23 10:19
浏览 484

PHP CURL错误:数据返回NULL值

I have a JSON code which I put in JSON file named data.json

the JSON Code is :

[
 {"value":"23","date":"03/2018"},
 {"value":"43","date":"03/2019"},
 {"value":"34","date":"12/2017"},
 {"value":"13","date":"01/2019"},
 {"value":"34","date":"02/2019"}
]

Now The php Code I am using to fetch the data is as Follows :

$url = "data.json" ;

$ch = curl_init() ;

curl_setopt($ch, CURLOPT_URL, $url) ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$result = curl_exec($ch);


curl_close($ch) ;


$data = json_decode($result, true) ;

foreach($data as $mydata)
{
  $data1 = $mydata["value"] ;
  $data2 = $mydata["date"] ;

echo $data1 ." : ". $data2 ;
echo "<br/>";

}

I Don't understand what's wrong with the code but it gives an error Warning

Invalid argument supplied for foreach()

This Happens Because the Data is returning NULL Value But I Don't How my JSON Data Format is not correct. THe Format Looks Fine. But it still returns NULL Value.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongliang1873 2019-04-23 10:58
    已采纳

    You have to provide the full URL of the JSON file if you are using CURL, instead of the data.json

    http://localhost/text.json 
    

    other options to get the data from json

    $result = file_get_contents($url);
    
    点赞 评论
  • donglu3087 2019-04-23 10:48

    when using cUrl with file name only, the cUrl think that it's an url so you will got error

    curl: (6) Could not resolve host
    

    if it's a local file, i will prefer to use file_get_contents...

    $json = file_get_contents("file.json");
    put here your json decode and logic
    

    and in case of url to use the curl.

    to answer and help you, let's try to see if you got any errors from curl...

    change the code like that and update us

    try to use curl_error and check if there are any errors from cUrl,

    what you get?

    $url = "data.json" ;
    $ch = curl_init() ;
    curl_setopt($ch, CURLOPT_URL, $url) ;
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $result = curl_exec($ch);
    
    if (curl_error($ch)) {
        $error_msg = curl_error($ch);
    }
    
    curl_close($ch) ;
    
    if (isset($error_msg)) {
        // TODO - Handle cURL error accordingly
    }
    
    // put here your json decode and logic
    
    点赞 评论
  • dtdvbf37193 2019-04-23 11:09

    It works fine for me.Please check.

    <?php
          $url = "http://raveesh.rnd/data.json" ;
          $ch = curl_init() ;
    
          curl_setopt($ch, CURLOPT_URL, $url) ;
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
          $result = curl_exec($ch);
          curl_close($ch) ;
    
          $data = json_decode($result, true) ;
    
          foreach($data as $mydata)
          {
               $data1 = $mydata["value"] ;
               $data2 = $mydata["date"] ;
    
               echo $data1 ." : ". $data2 ;
               echo "<br/>";
          }
    ?>
    
    Output:
    
     23 : 03/2018
     43 : 03/2019
     34 : 12/2017
     13 : 01/2019
     34 : 02/2019
    
    点赞 评论
  • dplece1882 2019-04-23 11:13

    The probleme here is :

    1- you can't curl the same host , you must use function file_get_content() to get your json like :

    <?php
    
    $string = file_get_contents("./data.json");
    $data = json_decode($string, true);
    
    
    
    foreach($data as $mydata)
    {
      $data1 = $mydata["value"] ;
      $data2 = $mydata["date"] ;
    
    echo $data1 ." : ". $data2 ;
    echo "<br/>";
    
    }

    if you want to use curl , it must be another host like:

    <?php
    
    $url = "http://date.jsontest.com" ;
    
    $ch = curl_init($url) ;
    
    $options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => array('Content-type: application/json')
    );
    
    curl_setopt_array( $ch, $options );
    $result = curl_exec($ch);
    
    curl_close($ch) ;
    
    $result = json_decode($result, 1);
    
    
    foreach($result as $key => $mydata)
    {
        echo $key ." : ". $mydata ;
        echo "<br/>";
    }

    so it depend of the structural of your Json

    thanks

    </div>
    
    点赞 评论

相关推荐 更多相似问题