php - Error on getting data from a JSON API -
currently i'd data json api url:
http://bitsnoop.com/api/trackers.php?hash=3ba918bf6b648bb6bec6aac716f1855451016980&json=1
i have tried following:
<?php $query = file_get_contents('http://bitsnoop.com/api/trackers.php?hash=3ba918bf6b648bb6bec6aac716f1855451016980&json=1'); $parsed_json = json_decode($query, true); foreach ($parsed_json $key => $value) { echo $value['announce']; echo "<br>"; } ?>
here i'd want value of as:
announce, num_seeders, num_leechers, found, updated.
but i'm getting error:
warning: invalid argument supplied foreach() in c:\xampp\test.php on line 4
you need add user agent headers
<?php function get_json($url, $curl = true) { $responsestring = ''; if (!$curl) { $responsestring = file_get_contents($url); } else { $ch = curl_init( $url ); $options = array( curlopt_returntransfer => true, curlopt_httpheader => array('content-type: application/json', 'user-agent: mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, gecko) chrome/39.0.2171.95 safari/537.36') , ); curl_setopt_array( $ch, $options ); $responsestring = curl_exec($ch); } return $responsestring; } $url ="http://bitsnoop.com/api/trackers.php?hash=3ba918bf6b648bb6bec6aac716f1855451016980&json=1"; $query = get_json($url); $parsed_json = json_decode($query, true); foreach ($parsed_json $key => $value) { echo $value['announce']; echo "<br>"; } ?>
Comments
Post a Comment