2

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

9
  • You can do using your json object Commented Feb 2, 2017 at 5:45
  • The error you get is pretty obvious, not? Warning: file_get_contents(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? Commented Feb 2, 2017 at 5:47
  • @Deep is there any way I can print full json response data? It will help me to parse data from resource. Commented Feb 2, 2017 at 5:47
  • @OfirBaruch Bro my site link is that the-worldwide.com How can I make it http:// I have to use https:// Commented Feb 2, 2017 at 5:50
  • You can either change your code (use cURL) or update your php configuration (stackoverflow.com/a/9791203/998096) Commented Feb 2, 2017 at 5:53

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks bro. u are right. the output data was not as json response. i solved that. thanks.
Whoever down voted please take out your precious time to mention the reason
0

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.

1 Comment

Thanks Deep for answer. I am trying it from my end. I will let you know soon. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.