I am trying to get data from http://api.stackoverflow.com/1.1/search?tagged=php .
I am using this code to get data from the API:
$url = "http://api.stackoverflow.com/1.1/search?tagged=php";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
print_r($json);
But it is showing me nothing. I have also used curl to get the data, but it also shows nothing. Why isn't it showing me anything, and how can I fix that?
They are returning you gzipped content as response. That's why it didn't work with your json decoding. Here is equivalent curl request.
$url= "http://api.stackoverflow.com/1.1/search?tagged=php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_ENCODING, ""); // this will handle gzip content
$result = curl_exec($ch);
curl_close($ch);
print $result;
// do json processing here
The response is gzipped, use gzdecode :
$url = "http://api.stackoverflow.com/1.1/search?tagged=php";
$json = file_get_contents($url);
$json_data = json_decode(gzdecode($json), true);
print_r($json);
Related
I want to fetch Api response (created in nodejs) in website using Php,So for this i am using
curl but its not working,I tried with following code but not working for me (showing blank page),Where i am wrong ? Here is my code
$post = ['email'=> "example#xyz.com",'password'=> "testing"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://35.154.149.228:8000/api/admin/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
$result = json_decode($response);
print_R($result);
Change
$result = json_decode($response);
To
$result = json_decode($response, true);
Then
echo '<pre>';
print_r($result);
Response:-
Array
(
[statusCode] => 401
[error] => Unauthorized
[message] => Invalid username or password
[responseType] => INVALID_USER_PASS
)
change your post array
$post = array('email'=> "example#xyz.com",'password'=> "testing");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://35.154.149.228:8000/api/admin/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
$result = json_decode($response);
print_r($result);
Try displaying errors just in case the errors/warnings are suppressed.
use these at the top of the file, just after the php tags
ini_set("display_errors", "On");
error_reporting(E_ALL);
Also try to print the raw response before json_decoding it, this is because if the response you are getting is not valid json nothing would be printed out after decoding it.
Use this
print_r("The response is: " . $response);
In summarry your code should look like
ini_set("display_errors", "On");
error_reporting(E_ALL);
$post = ['email'=> "example#xyz.com",'password'=> "testing"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://35.154.149.228:8000/api/admin/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
//Printing the original response before trying to decode it
//$result = json_decode($response);
print_r("The response from the server before decoding is: " . $response);
Let us know what the exact response you get from this is
I have created a call back API to receive the JSON data after hitting my server. But somehow its not working as expected
This the response which i have to receive in my API
{"ERROR":0,"STATUS":1,"ORDERID":753093,"OPTRANSID":"2107938600"}
I have written the php file for this rcstatus.php
$url = 'http://softwarecompany.club/pks/recharge/b2b/rcstatus.php?status='.$_GET['status'].'&Orderid='.$_GET['Orderid'].'&Optransid='.$_GET['Optransid'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 30000);
$data = curl_exec($ch);
echo $data;
$json = json_decode($url,true);
$json['STATUS'];
Please help me to achieve this. I have never worked with JSON data before. So please help me to achieve it. Thanks in advance!!!
You need to replace:
$json = json_decode($url,true);
with
$json = json_decode($data,true);
Replace $url to $data in last when you get the response like this.
$url = 'http://softwarecompany.club/pks/recharge/b2b/rcstatus.php?status='.$_GET['status'].'&Orderid='.$_GET['Orderid'].'&Optransid='.$_GET['Optransid'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 30000);
$data = curl_exec($ch); // This is Your Response
$json = json_decode($data,true);
$json['STATUS'];
I've been reading this helpful post:
http://techslides.com/hacking-the-google-trends-api
It shows how you can use cURL in command line/terminal to request data from google trends, for example;
curl --data "ajax=1&cid=actors&geo=US&date=201310" http://www.google.com/trends/topcharts/trendingchart
Gives you a big block of what I think is JSON. Below is an example of what I am doing to use cURL within my PHP to get data like this- however I cannot find anything that would get the data from the above cURL command to work in PHP like below.
<?php
//initialize session
$url = "http://www.google.com/trends/hottrends/atom/feed?pn=p1";
$ch = curl_init();
//set options
curl_setopt($ch, CURLOPT_URL, $url);
//execute session
$data = curl_exec($ch);
echo $data;
//close session
curl_close($ch);
?>
How do I go about getting the data from above?
You can do the same with the PHP cURL extension. You just need to set the options through curl_setopt, so you would do something like this
$url = "http://www.google.com/trends/topcharts/trendingchart";
$fields = "ajax=1&cid=actors&geo=US&date=201310";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
Now you have the response of the website in $data and you can do whatever you want with it.
You can find the PHP cURL documentation on http://php.net/curl
Try this
// Complete url with paramters
$url = "http://www.google.com/trends/topcharts/trendingchart?ajax=1&cid=actors&geo=US&date=201310";
// Init session
$ch = curl_init();
// Set options
curl_setopt($ch, CURLOPT_URL, $url);
// Set option to return the result instead of echoing it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute session
$data = curl_exec($ch);
// Close session
curl_close($ch);
// Dump json decoded result
var_dump(json_decode($data, true));
I am using the following code to get information about a repo using the Github API.
I am using cURL, but I'm not sure how to just get the name of the repository. So, how can I get just one string from the response and echo that without echoing the complete response? I tried doing $data['name'] but that didn't work.
code:
$ch = curl_init();
curl_setopt($ch,CURLOPT_USERAGENT,'Content-type: application/json');
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/repos/ruby/ruby');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
This request returns JSON, so just use json_decode.
$jsonStr = curl_exec($ch);
curl_close($ch);
$json = json_decode($jsonStr, true);
var_dump($json['name']);
var_dump($json['full_name']);
It should be trivial from here to get the elements you're interested in.
I am trying to hit an API and get some JSON back. I can’t seem to figure out what I am doing wrong here. I am getting null as the output.
Here is my PHP code:
<?php
$query_string_full = 'https://api.cityofnewyork.us/calendar/v1/search.htm?app_id=39563317lalaland&app_key=somethingsomething&categories=City%20Government%20Office';
$json = file_get_contents($query_string_full);
$obj = json_decode($json);
echo '<pre>'. json_encode($obj, JSON_PRETTY_PRINT) .'</pre>';
?>
The function file_get_contents doesn’t work with https. You should use the cURL functions instead.
<?php
$query_string_full = 'https://api.cityofnewyork.us/calendar/v1/search.htm?app_id=39563317&app_key=8396021a9bde2aad2eaf8ca9dbeca353&categories=City%20Government%20Office';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $query_string_full);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);
$obj = json_decode($json);
echo '<pre>'. json_encode($obj, JSON_PRETTY_PRINT) .'</pre>';
?>
I made this little function to return the json from a few different apis. You need to be using curl.
function exposeJSON ($apiUrl) {
$json_url = $apiUrl;
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $json_url);
// Built in authentication if needed.
//curl_setopt($ch, CURLOPT_USERPWD, "$USER:$PASS");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
// grab URL and pass it to the browser
$response = curl_exec($ch);
$return = json_decode($response[0], true);
// close cURL resource, and free up system resources
curl_close($ch);
return $return;
}
So you could do something like
$apiData = exposeJSON('urltoapi');
echo $apiData; // Should be the json.