简体   繁体   中英

Get json data with file_get_contents

I need to retrieve the information from this url :

http://example.com/ajax.php?sport=soccer&language_id=vn&block_id=page_home_1_block_home_matches_1&block_name=block_home_matches&callback_params={"date": "2016-01-14", "display": "all"}&action=updateContent&params={}

With my url like this:

http://example.net/ajax.php?sport=soccer&language_id=vn&block_id=page_home_1_block_home_matches_1&block_name=block_home_matches&callback_params=%7B%22date%22%3A%20%222016-01-14%22%2C%20%22display%22%3A%20%22all%22%7D&action=updateContent&params=%7B%7D

Here is ajax.php :

<?php

$sport = $_GET["sport"];
$language_id = $_GET["language_id"];
$block_id = $_GET["block_id"];
$block_name = $_GET["block_name"];
$callback_params = $_GET["callback_params"];
$action = $_GET["action"];
$params = $_GET["params"];

$url = "http://example.com/ajax.php?sport=$sport&language_id=$language_id&block_id=$block_id&block_name=$block_name&callback_params=$callback_params&action=$action&amp;params=$params";

echo file_get_contents($url);

?>

But is return nothing, what wrong here ??

The server might have allow_url_fopen set to Off for security (see http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen )

Try using curl to get the data instead

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
echo $data;

Separately to avoid possible issues with the data you're passing in the URL, be sure to call urlencode on each variable in case any contain special characters such as & or = . For example:

$sport = urlencode($_GET["sport"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM