URLをブラウザに入力すると、出力が得られます(以下)
{"categories":[{"id":"16","name":"Arm Locks"},{"id":"555","name":"Articles"},{"id":"19","name":"Back Control"},{"id":"21","name":"Brazilian Jiu Jitsu"},{"id":"12","name":"Chokes"},{"id":"15","name":"Crank"},{"id":"14","name":"Escapes"},{"id":"554","name":"Grappling Videos"},{"id":"10","name":"Guard and Rubber Guard"},{"id":"13","name":"Guard Breaks"},{"id":"23","name":"Half Guard"},{"id":"26","name":"Japanese Jujutsu"},{"id":"1","name":"Jiu Jitsu Training"},{"id":"5","name":"Jiu Jitsu Videos"},{"id":"25","name":"Leg Locks"},{"id":"27","name":"MMA"},{"id":"17","name":"Mount"},{"id":"30","name":"North South Position"},{"id":"556","name":"Platinum Members Only"},{"id":"29","name":"Self Defense"},{"id":"18","name":"Side Control"},{"id":"28","name":"Strikes"},{"id":"11","name":"Sweeps"},{"id":"20","name":"Theory & Discussions"},{"id":"9","name":"Throws"},{"id":"31","name":"Wrestling"},{"id":"22","name":"Wrist Locks"}]}
しかし、PHPを使用して解析しようとすると(PHPコードと出力)
PHP コード
<?php
$url = "https://domain.com/apis/cats.php?type=json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
$dcjsn = json_decode($json);
foreach ($dcjsn->categories as $cat){
echo $cat->id;
echo "<br>";
}
出力は次のとおりです。
555
15
14
554
13
23
26
25
27
17
30
29
18
28
20
9
22
つまり、約 10 個のアイテムが不足しています。
コードのエラーを特定できませんでした。file_get_contents() も試しましたが、うまくいきません。
添加:
var_dump($json)の結果
string(574) "{"categories":[{"id":"555","name":"Articles"},{"id":"15","name":"Crank"},{"id":"14","name":"Escapes"},{"id":"554","name":"Grappling Videos"},{"id":"13","name":"Guard Breaks"},{"id":"23","name":"Half Guard"},{"id":"26","name":"Japanese Jujutsu"},{"id":"25","name":"Leg Locks"},{"id":"27","name":"MMA"},{"id":"17","name":"Mount"},{"id":"30","name":"North South Position"},{"id":"29","name":"Self Defense"},{"id":"18","name":"Side Control"},{"id":"28","name":"Strikes"},{"id":"20","name":"Theory & Discussions"},{"id":"9","name":"Throws"},{"id":"22","name":"Wrist Locks"}]}"