Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get json using php

Tags:

I need to get the json data from, http://vortaro.us.to/ajax/epo/eng/ + 'word'+ "/?callback=?" working example (not enough reputation)

I know how to do it in javascript, But I need my php file to get this data, It needs to be server side, Thanks I'm new I have spent all day trying to figure this out. fopen and fread isn't working,

<?php $vorto = $_GET['vorto']; // Get the Word from Outer Space and Search for it!  if (isset($vorto))     {     echo $vorto;     } else {         $Help = "No Vorto -> add ?vorto=TheWordYouWant to the end of this website";         echo $Help;     } $url1 = "http://vortaro.us.to/ajax/epo/eng/";  $url2 = "/?callback=?"; $finalurl= $url1 . $vorto . $url2;  /* PLEASE HELP  $v1 = fopen($finalurl ,"r"); echo $v1;   $frv1 = fread($v1,filesize($v1)); echo $frv1 ;  */  ?> 
like image 490
Klanestro Avatar asked Jan 20 '10 09:01

Klanestro


People also ask

How get JSON data from API URL in PHP?

The php function file_get_contents($url) send a http request to the provided url and returns json data. The function json_decode($json) decodes the provided json string and returns as a PHP object. As simple as that you can parse json response. That was all about getting json from url in php.

Can you use JSON in PHP?

PHP has some built-in functions to handle JSON. First, we will look at the following two functions: json_encode() json_decode()

How can I get JSON data from URL?

To read a JSON response there is a widely used library called urllib in python. This library helps to open the URL and read the JSON response from the web. To use this library in python and fetch JSON response we have to import the json and urllib in our code, The json. loads() method returns JSON object.

How are JSON values accessed in PHP?

PHP File explained: Convert the request into an object, using the PHP function json_decode(). Access the database, and fill an array with the requested data. Add the array to an object, and return the object as JSON using the json_encode() function.


2 Answers

file_get_contents() can be used on a URL. A simple and convenient way to handle http page download.

That done, you can use json_decode() to parse the data into something useful.

like image 102
Berzemus Avatar answered Oct 12 '22 21:10

Berzemus


Take a look at PHP Curl.

With this example you are able to the all the informations.

<?php // create a new cURL resource $ch = curl_init();  // set URL and other appropriate options curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); curl_setopt($ch, CURLOPT_HEADER, 0);  // grab URL and pass it to the browser curl_exec($ch);  // close cURL resource, and free up system resources curl_close($ch); ?> 

Make sure that PHP Curl is enables in your php.ini. If you want to use fopen the setting allow_url_fopen must be 'ON' in your php.ini. Checkout phpinfo() for all the settings.

Since PHP 5.2.0 the function json_decode is part of the core.

like image 41
DrDol Avatar answered Oct 12 '22 21:10

DrDol