how can i fetch data from url location - php

I am working with the JSON in which I have to fetch the location of particular city e.g: http://maps.google.com/maps/geo?q=Vanguard,CA,%20United%20States&output=json
Now in PHP how can I write the code to fetch the "Point" data ?

So long as you have allow_url_fopen on in php.ini, you can use file_get_contents(). You can then parse with json_decode().
$json = json_decode(file_get_contents('http://maps.google.com/maps/geo?q=Vanguard,CA,%20United%20States&output=json'), TRUE);
$json is now an array of your JSON data.

$json_data=file_get_contents($url);
allow_url_fopen should be on in this case.
Curl can be used as an alternate option

I advice using the cURL lib for this. (cUrL). It just offers more options and error handling then using alternatives like file_get_contents etc.

Related

Take variables from a PHP script

I am making a website to display the data at https://api.captcoin.com/address/top/100. I need to be able to make the website take variables("address", "percent", "balance", and "rank") from this script and make them local variables in my site so I can display them. How can I take these variables and use them in my site?
First you need to get the remote page contents:
$remote = file_get_contents('link');
Then, since the data is in json format, you need to decode it using json_decode function.
$data = json_decode($remote, true);
true means that $remote should be decoded as associative array.
And finally you can access the data like an ordinary php array:
echo $data['top'][0]['address'];
Also, you should add some logic to handle situations when remote server is not accessible.
Use json_decode to convert the content of that url into an array and then search through it like you would through any array.
To get the actual content of the site please refer to this post Get file content from a URL?
You can either do it with javascript or php.
With javascript use this:
http://api.jquery.com/jquery.getjson/
You take the pages output and push them as variables to the php.
With php you can use
http://php.net/manual/en/function.json-decode.php
You make an array to push the json data into:
$object = array();
$json = file_get_contents('https://api.captcoin.com/address/top/100');
$object = json_decode ( string $json , true)
Be aware this is untested and read the json_decode api to customize the function-call.

Read specific remote line with PHP

I'm trying to find a way to load my latest tweet with PHP.
At this URL, I change "Twitter.xml" to my username-".xml."
https://twitter.com/users/show/Twitter.xml
The latest tweet is shown on line 49, but I don't think PHP can read a remote file... I want it to show line 49, but how do I combine it with finding a file on a remote server?
Thanks
In PHP you can use file_get_contents() to get the contents of that url. For example
$xml = file_get_contents('https://twitter.com/users/show/Twitter.xml');
This will only work if the config setting for allow_url_fopen is set to On
Use curl to fetch a remote resource.
http://www.php.net/manual/en/curl.examples-basic.php
http://www.php.net/manual/en/function.curl-init.php
Then, since it's xml, I would convert it to a native PHP object, using SimpleXML
http://www.php.net/manual/en/simplexml.examples-basic.php

How do I use external JSON...?

spent a few hours trying to figure this out, but cannot for the life of me figure out what's going wrong.
All I'm trying to do is load this:
https://recruit.zoho.com/ats/EmbedResult.hr?jodigest=2cV.Sr2As6VxhLMxQGuTNij*g.Fb3J7ysduDs.AC9sU-&atslocale=en_GB&rawdata=json
which I believe is json, into either javascript/jquery or php and use the data.
I've looked into jsonp, followed some tutorials, used some demos as templates and just can't get the above data to work.
If anyone can shed some light it would be much appreciated. It really shouldn't be this complicated, but I don't know what's going wrong.
Yep, that's JSON. The site may not support JSONP, so you're gonna have to use PHP to do this.
This is untested, but should work.
<?php
$url = 'https://recruit.zoho.com/ats/EmbedResult.hr?jodigest=2cV.Sr2As6VxhLMxQGuTNij*g.Fb3J7ysduDs.AC9sU-&atslocale=en_GB&rawdata=json';
$JSON = file_get_contents($url);
// echo the JSON (you can echo this to JavaScript to use it there)
echo $JSON;
// You can decode it to process it in PHP
$data = json_decode($JSON);
var_dump($data);
?>
JSONP relies on the server to return a JSONP formatted response. Basically, to use JSONP the server needs to return a JSON string wrapped in a function invocation ({"foo":1} becomes func({"foo":1})).
As the server your using doesn't return a JSONP response, you cannot use JSONP, you can only use JSON.
This is a shame, as JSON cannot be used x-domain due to the same origin policy (SOP). Therefore, the only option you have is to use a proxy server, which retrieves the JSON from the server, and either gives it to you in JSONP (see Yahoo Pipes), or which is on the same domain as the requested page (write a simple PHP script to get the file using file_get_contents() and then echo the output), in which case it can return the JSON.
I breifly looked at the requirements and it looks like you need an API key as well as an account. I saw that the site provides services for XML and JSON only. It looks to be fairly well documented.

How to request this URL and real the content of response using PHP?

I want to request this URL translate.google.com/translate_a/t?client=t&text=hello&hl=en&sl=en&tl=ar&multires=1&oc=3&prev=btn&ssel=0&tsel=0&sc=1
using PHP and reading the response. How ?
There are a number of options with cURL and readfile being the obvious ones.
You can use the file_get_contents() see http://php.net/manual/en/function.file-get-contents.php
But it is not as efficient as other methods to read remote files.
ie)
$mytranslation = file_get_contents("translate.google.com/translate_a/t?client=t&text=hello&hl=en&sl=en&tl=ar&multires=1&oc=3&prev=btn&ssel=0&tsel=0&sc=1");
Use the Google Translate API instead of requesting an HTML page and parsing the HTML.
Sample:
GET https://www.googleapis.com/language/translate/v2?key=YOUR-API-KEY&source=en&target=de&q=Your%20text

virustotal.com API example in PHP?

Does anybody have a PHP example of using the VirusTotal.com public API for URL scanning?
The API is available here:
http://www.virustotal.com/advanced.html
There's a Python/JSON example, but I'm not experienced with either :(
All you need is to retreive the report json using file_get_contents this way :
$json = file_get_contents('https://www.virustotal.com/api/get_url_report.json');
Then use json_decode to convert your json into a php array:
$array = json_decode($json);
to see results :
var_dump($array);
to post data use curl, related question.
A PHP example is available on that page (maybe it's new).

Categories