How do I call and decode a JSON web service from PHP? - php

From within PHP, how can I call an external JSON web service and then decode the returned string?
For example (pseudo-code):
<?php
$var jsonStr = json_decode("http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false");
?>

you almost had it!
<?php
$jsonObject = json_decode(file_get_contents("http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false"));
?>

Nathan has it. You may want to explore the curl library for a more robust HTTP request approach, too.

Related

How to run (api-request) a static URL in PHP and fetch response?

I have a static URL for a GET request, which is like - api.airtable.com/v0/<my-id>/VID?api_key=<api-key>. When I run this URL in chrome, I see a JSON response. But I want to fetch the response in PHP and work with it. How do I just run this simple api-request and get the JSON response in PHP?
I am a beginner to PHP, so this question might sound very basic to you :) Your help/advice is very appreciated - Thanks!
You can use the json_decode() function to convert your JSON into an array.
See documentation here: https://www.php.net/manual/en/function.json-decode.php
An example would be:
<?php
$json_data = file_get_contents("https://your-url-here");
$response_data = json_decode($json_data, true);
?>
Now your $response_data variable is an array, with which you can work normally.
Don't hesitate to ask if you need more help. :)
If JSON response is in array then you can use escapeJsonString() and json_decode() functions as below.
$response = escapeJsonString($response);
$response = json_decode($response,true);
print_r($response);

PHP Get REST Api and parse JSON

I have done research and I can't seem to find a solution. I have a website running XenForo and added XenAPI to the Software.
When you call the API: ( api.php?action=authenticate&username=USERNAME&password=PASSWORD)
it returns the following JSON:
{
"hash": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
}
I am trying to find the "proper" way of capturing the JSON and decoding it. I hope you guys can help! Note that I am looking for a example to learn from, because all other examples fail.
For simple requests with 1-2 params, you can use
$json = file_get_contents(url...);
$response = json_decode($json, true);
For more difficult queries use curl extension.
For full web-applications use Guzzle - curl wrapper for communicates with API.

PHP HTTP request to get JSON response

I am a novice in PHP. I have a URL and I need generate a GET request to this URL and get a JSON response. How might I achieve this?
You can perform GET requests using the following. . . provided PHP is not in safe mode.
file_get_contents();
curl();
You can use http://php.net/curl library to send GET requests.

Reading REST API Response in PHP

I am trying to read Raven SEO Tools API. It is a REST API and currently it is serving the data backup as an XML (or JSON if I choose) when I just request the URL through a web browser. What is the best method to get the response from their server into my own PHP script for me to then play around with.
Any help much appreciated
Cheers
If you only needs to retrieve a URL and parse its info. The easiest way is curl/JSON combination. Note that parsing JSON is faster than parsing XML.
http://www.php.net/manual/en/function.curl-exec.php
http://www.php.net/manual/en/function.json-decode.php
Something simple as:
$url = "http://api.raventools.com/api?key=B1DFC59CA6EC76FF&method=domains&format=json";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 4);
$json = curl_exec($ch);
if(!$json) {
echo curl_error($ch);
}
curl_close($ch);
print_r(json_decode($json));
But if you need to call other methods from this API such as DELETE/PUT, etc. Then to have a REST client in PHP is more elegant solution. A comparison on those clients can be found in PHP REST Clients
I founded this code specifically for Raven API https://github.com/stephenyeargin/raventools-api-php
Sample code:
require 'path/to/raventools-api-php/raventools-api-php.class.php';
$Raven = new RavenTools( 'B1DFC59CA6EC76FF' );
$method = 'domains';
$options = array('format'=> 'json');
$responseString = $Raven->getJSON($method, $options);
print_r(json_decode($responseString));
cUrl
cUrl is a command line tool for getting or sending files using URL syntax.
curl -o example.html www.example.com
file_get_contents
<?php
$homepage = file_get_contents('http://www.example.com/api/parameters');
echo $homepage;
?>
Pecl's HTTPRequest class is a very nice client, I've been using it for a couple of Projects. http://pecl.php.net/package/pecl_http
Another pretty cool client is the Buzz client https://github.com/kriswallsmith/Buzz
It also plays nice with Symfony2 if that's of interest to you :)
You can use either one of them, but I think JSON is the easiest and more hassle-free, unless you use SimpleXML. The decision depends on the complexity of your data.
Given that the JSON returned by the API is valid you can convert it to an array or object by using PHP's json_decode() function.
<?php
# retrieve JSON from API here...
# i.e. it is stored in $data as a string
$object = json_decode($data);
$array = json_decode($data, true);
?>
In SimpleXML, it would be as follows:
<?php
$object = simplexml_load_string($data);
?>

PHP How to SEND Json

The only way to send a JSON object is via JavaScript?
I'm developing a system that will have a lot of requests, send and receive Json.
If I can only work with Javascript to send the object, how do I trigger the js function?
Thanks in advance
[EDIT]
I have a C# working with database.
WCF making the communication between C# and PHP.
I was making the communication with WCF via SOAP, it was working fine but my boss want me to use JSON instead.
I've read some articles about JSON+PHP and the only way to send the JSON object is with JavaScript?
Once you have your JSON string encoded with json_encode() you simply echo it out with the correct MIME header.
header("Content-type: application/json");
echo json_encode($your_data);
to get the php file to return JSON you just make an array of the conditions and then json_encode echo it.....
response.php
$cond ? $response= array("conditions" => true) : $response = array("conditions"=>false);
echo json_encode($response);

Categories