显示使用php curl检索的json数据时出现问题
首先,谢谢你的阅读。这是我的密码 使用url在浏览器中查看我的json数据时的外观显示使用php curl检索的json数据时出现问题,php,json,curl,Php,Json,Curl,首先,谢谢你的阅读。这是我的密码 使用url在浏览器中查看我的json数据时的外观 { "$totalResults": 1500, "$startIndex": 1, "$itemsPerPage": 3, "$resources": [ { "$uuid": "5e7b9312-52e5-4fe1-b3e4-633ca04c9764", "$httpStatus": "OK", "$descriptor": "", "n
{
"$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;
};
?>
我不确定出了什么问题,如果我从一个文件中获得相同的数据,但如果我从rest服务器检索数据,这是不可行的,如果有人能告诉我一些情况,我将非常感激PHP json_decode函数返回一个数组而不是一个对象,所以不要使用
foreach ($json->{'$resources'} as $mydata) {
echo $mydata->name;
};
你应该使用
foreach ($json['$resources'] as $mydata) {
echo $mydata['name'];
};
更新
您的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变量,这导致了问题
将解码后的变量转换为数组可以解决问题,因为关联数组键是字符串。PHP json\u decode函数返回数组而不是对象,因此不使用
foreach ($json->{'$resources'} as $mydata) {
echo $mydata->name;
};
你应该使用
foreach ($json['$resources'] as $mydata) {
echo $mydata['name'];
};
更新
您的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变量,这导致了问题
将解码的变量转换为数组可以解决问题,因为关联数组键是字符串。Ajking11为foreach提供的参数无效的原因是$json variable被覆盖,没有数据。请尝试类似的操作
<?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.
}
请注意,我已经删除了&format=json,并在稍后进行了编码。希望这有助于让我了解您的进展情况。11您获取foreach提供的无效参数的原因是$json VARIABLE被覆盖,没有数据。请尝试类似的操作
<?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.
}
请注意,我已经删除了&format=json并在以后进行了编码。希望这有助于我了解您的进展情况。您可能希望使用此代码并根据需要对其进行优化
<?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在名称空间问题上提供的帮助您可能希望使用此代码并根据需要对其进行优化
<?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在名称空间问题上的帮助,因为巧妙的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>";
}
正如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>";
}
我真的很抱歉再也没有回复你。因此,当您从sage服务获取数据时,它会以utf 8 bom的形式返回给您 因此,在显示任何内容之前,我需要通过清除数组中的前三个字符来删除格式。使用substr函数
$json_data = substr($json_data, 3);
谢谢大家帮我解决这个问题。我真的很抱歉再也没有给你们回复。因此,当您从sage服务获取数据时,它会以utf 8 bom的形式返回给您 因此,在显示任何内容之前,我需要通过清除数组中的前三个字符来删除格式。使用substr函数
$json_data = substr($json_data, 3);
感谢大家帮助解决这个问题。检查$json和$result变量中的内容。不确定要使用什么,我做了一个版本转储isset;和&results里面有东西,但是$json不知道是怎么回事。检查$json和$result变量中的内容。不确定要使用什么,我做了一个版本转储isset;and&results中有内容,但$json不知道是怎么回事。更改foreach返回:警告:第29行的C:\xampp\htdocs\Sage json\test2.php中为foreach提供的参数无效。您是否能够解析导致上述响应的json文件开头的无效字符更改foreach返回:警告:第29行的C:\xampp\htdocs\Sage Json\test2.php中为foreach提供的参数无效。您是否能够解析导致上述响应的Json文件开头的无效字符?我已经对此做了更多的研究,$Json\u decode的$result部分似乎不是有效的Json代码,因此返回nullI对此做了更多的研究,似乎$json_decode的$result部分不是有效的json代码,因此返回null