2015-04-22 90 views
1

首先,感謝您的閱讀。這裏是我的代碼顯示使用php curl檢索json數據的問題

什麼的json數據看起來使用的URL在瀏覽器中查看時一樣,

{ 
    "$totalResults": 1500, 
    "$startIndex": 1, 
    "$itemsPerPage": 3, 
    "$resources": [ 
    { 
     "$uuid": "5e7b9312-52e5-4fe1-b3e4-633ca04c9764", 
     "$httpStatus": "OK", 
     "$descriptor": "", 
     "name": "Heinz Tomato Sauce Sachets Qty 200" 
    }, 
    { 
     "$uuid": "1a0f9dca-c417-4cff-94d2-99d16438723f", 
     "$httpStatus": "OK", 
     "$descriptor": "", 
     "name": "Heinz Brown Sauce Sachet Qty 200" 
    }, 
    { 
     "$uuid": "126fdb17-81ce-41b4-bdba-91d0a170262a", 
     "$httpStatus": "OK", 
     "$descriptor": "", 
     "name": "Heinz English Mustard Sachets Qty 300" 
    } 
    ] 
} 

test2.php

<?php 

// SAGE URL 
$url = 'http://localhost:5493/sdata/accounts50/GCRM/-/commodities?select=name&count=3&format=json'; 

//SAGE USERNAME & PASSWORD 
$username = 'test'; 
$password = 'test'; 


    //initiaize 
$ch = curl_init(); 

    //set options  
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password); 
curl_setopt($ch, CURLOPT_HTTPGET, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HEADER, 0);   

    //execute 
$result = curl_exec($ch); 

    //close curl session/free resources 
curl_close($ch); 

$json = json_decode($result); 

foreach ($json->{'$resources'} as $mydata) { 
    echo $mydata->name; 
}; 

?> 

錯誤即時得到

Notice: Trying to get property of non-object in C:\xampp\htdocs\Sage Json\test2.php on line 29 

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Sage Json\test2.php on line 29 

我不確定發生了什麼問題,如果我從文件中獲得相同的數據,但是如果我從資源中獲取相同的數據牛逼的服務器,如果有人可以提供一些線索我將不勝感激

+0

檢查$ json和$ result變量中的內容。 – mohit

+0

不確定要使用什麼,我做了一個ver_dump(isset());和&結果裏面有東西,但$ json不知道怎麼回事。 – Ajking11

回答

0

IM爲從不又回到了你真的很對不起你的幫助。所以事實證明,當你從sage服務獲取數據時,它會以「utf 8 bom」的形式返回給你。

因此,在我可以顯示任何我需要通過清除數組中的前三個字符來刪除格式。與substr()函數。

$json_data = substr($json_data, 3); 

謝謝大家幫忙解決這個問題。

1

PHP json_decode函數返回一個數組,而不是一個對象,所以,而不是使用

foreach ($json->{'$resources'} as $mydata) { 
    echo $mydata->name; 
}; 

你應該使用

foreach ($json['$resources'] as $mydata) { 
    echo $mydata['name']; 
}; 

UPDATE

你的test2.php應該看起來像這樣:

<?php 

// SAGE URL 
$url = 'http://localhost:5493/sdata/accounts50/GCRM/-/commodities?select=name&count=3&format=json'; 

//SAGE USERNAME & PASSWORD 
$username = 'test'; 
$password = 'test'; 


//initiaize 
$ch = curl_init(); 

//set options 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password); 
curl_setopt($ch, CURLOPT_HTTPGET, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HEADER, 0); 

//execute 
$result = curl_exec($ch); 

//close curl session/free resources 
curl_close($ch); 

$json = json_decode($result); 
$json = (array)$json; 

foreach ($json['$resources'] as $mydata) { 
    echo $mydata->name . "<br>"; 
}; 

在您的代碼中,主要問題在於命名JSON屬性$resources:它被命名爲PHP變量並導致該問題。

將解碼變量轉換爲數組解決了問題,因爲關聯數組鍵是一個字符串。

+0

更改foreach返回: '警告:在C:\ xampp \ htdocs \ Sage Json \ test2.php上爲第29行中的foreach()提供了無效參數 – Ajking11

+0

您是否能夠在您的開頭解析無效字符json文件,這是造成上述響應 –

2

Ajking11你獲得了的foreach()提供了無效的說法是,$ JSON vairiable被覆蓋,是有原因的沒有數據嘗試財產以後像我已刪除&格式= JSON和後來的編碼這個

<?php 

// SAGE URL 
$url = 'http://localhost:5493/sdata/accounts50/GCRM/-/commodities?select=name&count=3'; 

//SAGE USERNAME & PASSWORD 
$username = 'test'; 
$password = 'test'; 


//initiaize 
$ch = curl_init(); 

//set options 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password); 
curl_setopt($ch, CURLOPT_HTTPGET, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HEADER, 0); 

//execute 
$result = curl_exec($ch); 

//close curl session/free resources 
curl_close($ch); 
$xml = simplexml_load_string($result); 
$data = json_encode($xml); 
$arr = json_decode($data,TRUE); 


foreach ($arr['entry'] as $k=>$v){ 
    echo $v; // etc. 
} 

記希望這有助於讓我知道你的近況如何

+0

我一直在做這個多一點研究,它似乎$ json_decode的$結果部分是無效的json代碼,所以返回null –

2

你可能想使用此代碼,並將其細化到您的需求

<?php 
    // SAGE URL 
     //$url = 'http://computer_2:5493/sdata/accounts50/GCRM/-/commodities?select=name&count=3&select=name&count=10&format=json'; 
     $url ='http://localhost:5493/sdata/accounts50/GCRM/-/tradingAccounts?select=name&count=10'; 
     //SAGE USERNAME & PASSWORD 
     $username = 'MANAGER'; 
     $password = ''; 

     //initiaize 
     $ch = curl_init(); 

     //set options 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password); 
     curl_setopt($ch, CURLOPT_HTTPGET, true); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_HEADER, 0); 

     //execute 
     $result = curl_exec($ch); 

     //close curl session/free resources 
     curl_close($ch); 
     $xml = simplexml_load_string($result); 



foreach($xml->xpath("//sdata:payload") as $entry) { 
    // xpath here must be from payload to tradingAccount 
    $content = $entry->xpath("./crm:tradingAccount"); 
    foreach($content as $c) { 
     // Make set of children with prefix crm 
     $nodes = $c->children('crm', true); 
     echo $nodes->reference . " | " . $nodes->name . "/" . $nodes->openedDate . "<br />\n"; 

    } 
} 

感謝PHPhil和splash58與命名空間問題

2

由於Artful_dodger說是JSON的是這裏的問題是我會怎麼解決呢

<?php 

// SAGE URL 
$url = 'http://localhost:5493/sdata/accounts50/GCRM/-/commodities?select=name&count=3&format=json'; 

//SAGE USERNAME & PASSWORD 
$username = 'MANAGER'; 
$password = ''; 


//initiaize 
$ch = curl_init(); 

//set options 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password); 
curl_setopt($ch, CURLOPT_HTTPGET, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HEADER, 0); 

//execute 
$result = curl_exec($ch); 

//close curl session/free resources 
curl_close($ch); 

// doing a substring as there are 3 weird characters being passed back from IIS in front of the string 
$json = json_decode(substr($result, 3, strlen($result)), true); 


    foreach ($json['$resources'] as $mydata) { 
     echo $mydata['name'] . "<br>"; 
    }