Lemon Kazi Lemon Kazi - 4 years ago 158
JSON Question

JSON response through URL not displaying

Hi I have a page link with json data.
I wanted to fetch them from url.
I tried below code. But that not working for me.

echo $URL1 = "https://www.the-worldwide.com/wp-content/themes/thewebconz/Live-6-cid/functions.php";
$vars1 = "action=Hotel_Description&hotel_id=438271&cid=457428&apiKey=5jjdvgnq9aug1a4ucvatvq4b8u&ModeType=Live&secret=9hs897nn3e9av";
$URLs_Fetch1 = $URL1."?".$vars1;

$json = file_get_contents($URLs_Fetch1);

$data = json_decode($json,true);

$Geonames = $data;

echo "<pre>";


But if you normally visit that page you can see many json data there.



Answer Source

If you use jsonlint to validate the response of the url directly from browser you will find that the output is not a valid json response. hence you are unable to decode it. All you need to do is remove html tags which are hidden and hotel_id content and and issues with new lines . After solving those issues it becomes a valid json response. and there you go.

$base_url = "https://www.the-worldwide.com/wp-content/themes/thewebconz/Live-6-cid/functions.php";
$data = [
    "action" => "Hotel_Description",
    "hotel_id" => 438271,
    "cid" => 457428,
$config = [
    "apiKey" => "5jjdvgnq9aug1a4ucvatvq4b8u",
    "ModeType" => "Live",
    "secret" => "9hs897nn3e9av"
function buildUrl($base_url, $data, $config)
    $url = $base_url."?";
    $data_uri = "";
    foreach ($data as $data_key => $data_value) {
        $data_uri .= "$data_key=$data_value&";
    $config_uri = "";
    foreach ($config as $config_key => $config_value) {
        $config_uri .= "$config_key=$config_value&";
    $url= $base_url."?".$data_uri.rtrim($config_uri, "&");
    return $url;
$url = buildUrl($base_url, $data, $config);
$content = file_get_contents($url);
$new_content = str_replace("<HotelInformationRequest><hotelId>".$data['hotel_id']."</hotelId><options>0</options></HotelInformationRequest>", "", $content);
$json_content = str_replace("\n", " ", $new_content);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download