21

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 ;
*/
?>
asked Jan 20, 2010 at 9:34

3 Answers 3

61

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.

answered Jan 20, 2010 at 9:40
Sign up to request clarification or add additional context in comments.

3 Comments

file_get_contents()? is that requiring an another php modules to be installed? :D
@gumuruh: file_get_contents() is built-in to PHP. :-)
Using curl is a lot faster and will print better results. You can set timeouts caching and more. I will never use file_get_contents for api-request // Source -> Google: curl vs file_get_contents
14

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.

answered Jan 20, 2010 at 9:44

Comments

1

Old question, but still one of the top hits in Google, so here is my contribution on top of @DrDol's answer.

<?
 $url = "http://www.example.com/api/v1/endpoint";
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
 $data = json_decode(curl_exec($ch)); 
 curl_close($ch);
?>

Note the user of the CURLOPT_RETURNTRANSFER which sends the response into the return value (and returns false on fail).

answered Dec 12, 2015 at 14:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.