通过URL不显示JSON响应

时间:2017-02-02 05:41:05

标签: php json wordpress api

您好我有一个json数据的页面链接。 我想从网址上获取它们。 我试过下面的代码。但那不适合我。

<?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);     
?>

但是如果您通常访问该页面,则可以在那里看到许多json数据。

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

2 个答案:

答案 0 :(得分:2)

如果使用jsonlint直接从浏览器验证url的响应,您会发现输出不是有效的json响应。因此你无法解码它。您需要做的就是删除隐藏的html标记和hotel_id内容以及新行的问题。解决了这些问题后,它就变成了有效的json响应。你去吧。

<?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);

答案 1 :(得分:0)

安装php5-openssl之后重启Apache。 SO关于此问题已有答案,

如果不起作用,请将extension=php_openssl.dll添加到位于php.inixampp/php/php.ini文件中的wamp\path to php.ini文件中。

即。只需在php.ini文件中添加两行。

extension=php_openssl.dll
allow_url_include = On

如果这些对您没有帮助,请告诉我。