Jump to content

Display JSON from URL


M_cMoreland

Recommended Posts

OK I am a bit of newb so forgive me. However I am trying to create a page that I can send an ID to and display the JSON of a url this page builds a new url (for another domain) I want to display the JSON from the new url. 

 

CODE:

<?php

//Get Client IP and Set $client_ip 
//if ( isset($_SERVER["REMOTE_ADDR"]) )    { 
//$client_ip=$_SERVER["REMOTE_ADDR"]; 
//} else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) )    { 
//$client_ip=$_SERVER["HTTP_X_FORWARDED_FOR"]; 
//} else if ( isset($_SERVER["HTTP_CLIENT_IP"]) )    { 
//$client_ip=$_SERVER["HTTP_CLIENT_IP"];   
//} 
//Get listing ID from URL filter_input(INPUT_GET, 'listing_id', FILTER_SANITIZE_STRING);
$listing_id = '10100230';
$client_ip = '123.4.56.78';
//Set Static Variables
$publisher = 'test';
$format = 'JSON';
$placement = 'xpage';

//Create query_string array for URL
$query_string = array('listing_id' => $listing_id,
                      'placement' => $placement,
                      'client_ip' => $client_ip,
                      'publisher' => $publisher,
                      'format' => $format
                           
                      );

//Build url
$url = "http://api.citygridmedia.com/content/places/v2/detail?" .
        http_build_query($query_string, '', "&");


  print $url 

?>

Right now it works to build the url what do I need to do to pull the json from the new URL and display it in the same or another page?

 

Here is the URL that is built and it works in a browser.

 

http://api.citygridmedia.com/content/places/v2/detail?listing_id=10100230&placement=test_page&client_ip=123.4.56.78&publisher=test&format=JSON

 

MOD EDIT: code tags added.

Link to comment
https://forums.phpfreaks.com/topic/243012-display-json-from-url/
Share on other sites

If you are asking how to get the JSON, you could use an ajax/javascript approach, or use php's file_get_contents function. You could also use cURL. Once the json is returned, you would either deal with it though javascript if you choose that approach, or use php's json_decode function, which turns the json back into an array.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.