我在这里问了一个类似的问题:How do i parse Json data from openlibrary api? (properly)
然而,似乎我从那里获得的代码似乎能够解析来自Wikia.com的json。我看过api并且设法获得了一些Json,到目前为止我已经设法做了以下事情:
<?php
$url = "http://yugioh.wikia.com/api/v1/Search/List/?query=stardust&limit=25&namespaces=0%2C14&format=json";
$headers = array( "Content-type: application/json;charset=\"utf-8\"", "Accept: text/xml", "Cache-Control: no-cache", "Pragma: no-cache", "SOAPAction: \"run\"" );
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, $headers);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($cURL);
var_dump($result);
foreach (json_decode($result, true) as $book)
{
printf("\nId: %s\tTitle: %s\tUrl: %s", $book['items']['id'][0], $book['items']['title'], $book['items']['url']);
switch (json_last_error())
{
case JSON_ERROR_NONE:
echo ' - No errors<br><br>';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded<br><br>';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch<br><br>';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found<br><br>';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON<br><br>';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded<br><br>';
break;
default:
echo ' - Unknown error<br><br>';
break;
}
}
?>
我使用了var_dump($ results)来查看在我到达foreach循环之前的最新情况:
string(3325) "{"total":2670,"batches":107,"currentBatch":1,"next":26,"items":[{"id":79895,"title":"Stardust Dragon","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Dragon","ns":0,"quality":99},{"id":245281,"title":"Stardust Shimmer","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Shimmer","ns":0,"quality":91},{"id":197443,"title":"Stardust (disambiguation)","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_(disambiguation)","ns":0,"quality":16},{"id":461985,"title":"Stardust (archetype)","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_(archetype)","ns":0,"quality":68},{"id":202487,"title":"Stardust Mirage","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Mirage","ns":0,"quality":84},{"id":157331,"title":"Stardust Overdrive","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Overdrive","ns":0,"quality":98},{"id":310759,"title":"Stardust Battle","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Battle","ns":0,"quality":82},{"id":319208,"title":"Stardust Overdrive Plus","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Overdrive_Plus","ns":0,"quality":92},{"id":161435,"title":"Stardust Xiaolong","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Xiaolong","ns":0,"quality":95},{"id":237946,"title":"Stardust Phantom","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Phantom","ns":0,"quality":95},{"id":486075,"title":"Stardust Warrior","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Warrior","ns":0,"quality":94},{"id":476844,"title":"Stardust Re-Spark","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Re-Spark","ns":0,"quality":94},{"id":163366,"title":"Stardust Flash","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Flash","ns":0,"quality":93},{"id":231866,"title":"Stardust Divinity","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Divinity","ns":0,"quality":87},{"id":400288,"title":"Token (Stardust Spark Dragon)","url":"http:\/\/yugioh.wikia.com\/wiki\/Token_(Stardust_Spark_Dragon)","ns":0,"quality":86},{"id":317827,"title":"Stardust Spark Dragon","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Spark_Dragon","ns":0,"quality":92},{"id":156235,"title":"Ensure Stardust Dragon's return","url":"http:\/\/yugioh.wikia.com\/wiki\/Ensure_Stardust_Dragon%27s_return","ns":0,"quality":45},{"id":132165,"title":"Stardust Dragon\/Assault Mode","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Dragon\/Assault_Mode","ns":0,"quality":98},{"id":196680,"title":"Malefic Stardust Dragon","url":"http:\/\/yugioh.wikia.com\/wiki\/Malefic_Stardust_Dragon","ns":0,"quality":97},{"id":216824,"title":"Stardust Dragon (character)","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Dragon_(character)","ns":0,"quality":96},{"id":259230,"title":"Malefic Stardust Dragon (character)","url":"http:\/\/yugioh.wikia.com\/wiki\/Malefic_Stardust_Dragon_(character)","ns":0,"quality":92},{"id":516389,"title":"Stardust Charge Warrior","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Charge_Warrior","ns":0,"quality":84},{"id":469151,"title":"Stardust Dragon (Duel Arena)","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Dragon_(Duel_Arena)","ns":0,"quality":97},{"id":493911,"title":"Stardust Spark Dragon (manga)","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Spark_Dragon_(manga)","ns":0,"quality":89},{"id":494358,"title":"Stardust Dragon (anime)","url":"http:\/\/yugioh.wikia.com\/wiki\/Stardust_Dragon_(anime)","ns":0,"quality":95}]}"
可悲的是,我加载页面时得到的唯一输出是
[ - 上面Var_dump的字符串 - ] Id:标题:网址: - 没有错误
Id:标题:网址: - 没有错误
Id:标题:网址: - 没有错误
Id:标题:网址: - 没有错误
Id:标题:网址: - 没有错误
我试过了:
...
printf("\nId: %s\tTitle: %s\tUrl: %s", $book['items']['id'], $book['items']['title'], $book['items']['url']);
...
答案 0 :(得分:1)
查看json对象的属性。它包含
"total":2670,
"batches":107,
"currentBatch":1,
"next":26,
"items":[...]
您的代码循环遍历每个属性而不是items数组。您的print语句也在访问不正确的索引。
$json = json_decode($result, true);
foreach ($json['items'] as $book)
{
printf("\nId: %s\tTitle: %s\tUrl: %s", $book['id'], $book['title'], $book['url']);
}
http://sandbox.onlinephpfunctions.com/code/f0f9ed981148cd5a2abaa5b5ce0a6292cca5740c