Lemon Kazi
Lemon Kazi

Reputation: 3311

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.

<?php
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>";

print_r($Geonames);     
?>

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

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

Demo

Upvotes: 2

Views: 856

Answers (2)

M A SIDDIQUI
M A SIDDIQUI

Reputation: 2205

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.

<?php
$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, "&");
    unset($data_uri);
    unset($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);

Upvotes: 2

Deep Kakkar
Deep Kakkar

Reputation: 6297

Install php5-openssl Restart Apache afterwards. There is already an answer on SO about this,

if not worked then add extension=php_openssl.dll to your php.ini file located in xampp/php/php.ini or wamp\path to php.ini file.

i.e. just add two lines in your php.ini file.

extension=php_openssl.dll
allow_url_include = On

If these will not help you out then please let me know.

Upvotes: 0

Related Questions