2016-04-08 98 views
0

我想將數據從ios傳遞到服務器。Json_decode返回字符串(0)「」

PHP代碼:

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

這給了我string(0) ""。我不知道這是什麼意思。

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

當我打印的JSON字符串它給了我一個有效的字符串

請找我的另一個問題,這個問題的全部代碼

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

+0

'$ inputJSON'是否有值? – aldrin27

+0

echo $ inputJSON;什麼也沒給 var_dump($ inputJSON);返回字符串(0)「」 – Lydia

+0

php://輸入這是我以前工作。 – Lydia

回答

1

在上帝恩典解決了我的problem-

通真正轉換對象關聯數組,所以訪問數字/關聯數組是這樣的。

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

然後,我們使用數字和關聯數組語法的組合來訪問多維數組中的所需元素。

但是,如果我想var_dump($數據); retun null。

參考教程:http://www.dyn-web.com/tutorials/php-js/json/decode.php

2

我已經創造了你,我講一個例子,響應使用捲曲功能: -

1.My兩個文件(php代碼文件和json字符串文件),在相同的工作directory.Check: - http://prntscr.com/apkbp9

2. json字符串文件內容應該是: -

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

檢查這裏: - http://prntscr.com/apkbx0

3. php代碼文件的內容: -

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

入住這裏: - http://prntscr.com/apkc6x

輸出上我的瀏覽器: - http://prntscr.com/apkcoi

您可以使用CURL像如下: -

<?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); 

?> 

更多參考有關CURL: - http://php.net/manual/en/book.curl.php

注: - 如果兩個都不行,那麼首先您需要保存在擴展.txt文件您json數據,然後你可以使用上面給出這兩個代碼,只需通過放置該文本文件的完整路徑。

+0

是的,我做了同樣的事情,如果你看我的問題意味着你可以看到。 – Lydia

+0

from ios to php,we can not define a php file.the http post taken as file_get_contents('php:// input'); – Lydia

+0

我認爲可能是問題是我發送json數組,所以可能是無效的 – Lydia