dqpu4988
2016-04-08 05:18
浏览 169

Json_decode返回字符串(0)“”

I am trying to pass data from ios to server.

Php code :

<?php
   $inputJSON = file_get_contents('php://input');
   $data = json_decode($inputJSON, TRUE);
   var_dump($data);
?>

This gives me string(0) "". I don't know whats the meaning of this.

        echo $inputJSON; gives nothing 
       var_dump($inputJSON); returns  string(0) ""

When i print the json-string it gives me a valid string

Please find the full codes of this problem in another question of mine

Cannot able to access json array in php , it returns null and Warning: Invalid argument supplied for foreach()

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将数据从ios传递到服务器。

Php代码 :

 &lt;?php 
 $ inputJSON = file_get_contents('php:// input'); 
 $ data = json_decode($ inputJSON,TRUE); 
  var_dump($ data); 
?&gt; 
   
 
 

这给了我 string(0)“”。 我不知道这是什么意思。

  echo $ inputJSON; 什么都不给
 var_dump($ inputJSON); 返回字符串(0)“”
   
 
 

当我打印json-string时,它给我一个有效的字符串

请查找 我的另一个问题中这个问题的完整代码

无法访问php中的json数组,它返回null并且警告:为foreach()提供的参数无效

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

3条回答 默认 最新

  • dounielong7728 2016-04-08 06:37
    已采纳

    With God Grace solved my problem-

    pass true to convert objects to associative arrays, so accessing numeric/associative array like this.

    $var = $data[0]['key'];
    

    Then we use a combination of numeric and associative array syntax to access the desired element in multidimensional array.

    But if i trying to var_dump($data); is retun null.

    Reference tutorial: http://www.dyn-web.com/tutorials/php-js/json/decode.php

    已采纳该答案
    打赏 评论
  • dongshao8566 2016-04-08 05:47

    fisrt you should check is your url is correct or not. If every thing is right but still its not showing the response use curl functionality such as alter native of file_get_content

    打赏 评论
  • doujia4041 2016-04-08 05:48

    I have created an example for you that what i am talking:-

    1.My both file (php code file and json string file) in the same working directory.Check:- http://prntscr.com/apkbp9

    2.json string file content should be:-

    [
        {
            "email" : "",
            "Name" : "Ddd",
            "contact2" : "",
            "ontact1" : ""
        },
        {
            "email" : "",
            "Name" : "Ddd",
            "contact2" : "",
            "contact1" : ""
        },
        {
            "email" : "",
            "Name" : "Dddddr",
            "contact2" : "",
            "contact1" : ""
        }
    ]
    

    Check here:- http://prntscr.com/apkbx0

    3.php code file content:-

    <?php
       $inputJSON = file_get_contents('input.txt');
       echo $inputJSON;
       $data = json_decode($inputJSON, TRUE);
       echo "<pre/>";print_r($data);
    ?>
    

    Check here:- http://prntscr.com/apkc6x

    Output on my browser:- http://prntscr.com/apkcoi

    You can use CURL like below:-

    <?php
       function url_get_contents ($Url) {
            if (!function_exists('curl_init')){ 
                die('CURL is not installed!');
            }
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $Url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $output = curl_exec($ch);
            curl_close($ch);
            return $output;
        }
        $inputJSON = url_get_contents('php://input');
        echo $inputJSON;
        $data = json_decode($inputJSON, TRUE);
        echo "<pre/>";print_r($data);
    
    ?>
    

    For more reference about CURL :- http://php.net/manual/en/book.curl.php

    Note:- if both will not work, then first you need to save your json data in a file with extension .txt and then you can use both code given above, just by putting the full path of that text file.

    打赏 评论

相关推荐 更多相似问题