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.
Related
I want to check all of the requested urls and if the url contains "video" folder in it, redirect it to an API file. then the API gives me a json files which only contains "respond:true" or "respond:false". In case of having respond:true in the json file the url must be showed and if the json file contains respond:false a predefined simple 403 page must be showed to the user.
I know that the fist part is possible with a simple code in .htaccess file like this:
RewriteRule ^your/special/folder/ /specified/url [R,L]
But I don't know how to do the second part. I mean how to get the result of API, which is in form of a json file and check it.
You can use CURL..
GET REQUEST
$url = 'http://example.com/api/products';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_json = curl_exec($ch);
curl_close($ch);
$response=json_decode($response_json, true);
POST REQUEST
$postdata = array(
'name' => 'Arfan'
);
$url = "https://example.com/api/user/create";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
You can also use file_get_content to get API data.
$json = file_get_contents("$url")
You can execute second part (Calling API and response): Call API using curl and process based on its response:
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_URL, "api_url_here");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$api_response_json = curl_exec($ch);
curl_close($ch);
//convert json to PHP array for further process
$api_response_arr = json_decode($api_response_json);
if($api_response_arr['respond'] == true ){
//code for success here
}else{
// code for false here
}
Please note: Json response from API is depend on API Response, If API is giving response in json format ( can be based on params also ).
I have a Python script which gives me back some data in JSON. It starts a session, posts some auth data and requests data which comes back with JSON. That works fine, but can somebody help me to do this in PHP? I am sure it is possible but I am struggling to construct that.
import requests
with requests.Session() as s:
start_time = time.time()
s.post('http://IP:PORT/WHATEVER/AUTH', data={'username':'jdoe','password':'forgotten'})
req = 'http://IP:PORT/WHATEVER/DATA/BOM%20fe?table=cars,tires,&format=json'
res = s.get(req)
you need to look for curl request in php. here is a example
function getdata($url){
if(!function_exists("curl_init"))
die("cURL extension is not installed");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
curl_close ($ch);
return $output;
}
$output = getdata("https://website.com");
var_dump($output);
you can use json decode fuction to covert into matrix and use the variables
$arr = json_decode($output,true);
you can use something like this for formatting:
curl_setopt($ch, CURLOPT_POSTFIELDS,
http_build_query(array('postvar1' => 'value1')));
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 want to get Json data from Carquery API. But none of my code worked.
First i tried to get the data using curl. This is my code
$loginUrl = 'http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$loginUrl);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result));
It returned null. And then i tried using file_get_content. This is my code :
$json = file_get_contents('http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015');
$obj = json_decode($json);
var_dump($obj);
Unfortunately it didn't work either.
Any help please?
First of all try this:
$loginUrl = 'http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$loginUrl);
$result=curl_exec($ch);
if($result === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
var_dump(json_decode($result));
}
curl_close($ch);
so you can see error if any on curl request.
and 2nd thing that url:
'http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015'
are you sure that the must be 2 (two) ? (question marks) ?
json data return from that API is not correct, i tried using jsonlint.com to check it.
If the callback part is removed from
'http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015';
into
'http://www.carqueryapi.com/api/0.3/?cmd=getTrims&year=2015';
it will produce right json and can be parsed either using file_get_content() or curl.
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);