Get json from API using PHP - php

I have this API
mywebsite.com/v1/api/Endorsements/xxxxxx/User/5003C2D5-6FD6-49BA-A309-EBB81A89FB60/RequestType/All/PageNo/1/PageSize/6/Object/null/Category/null/Search/null/Latitude/null/Longitude/null/Radius/null/StartDate/null/EndDate/null?Auth-Token=A682CCB8-C5D6-47C3-BDF5-8EEC00A164ABapi.salamplanet.com/v1/api/Endorsements/81158/User/5003C2D5-6FD6-49BA-A309-EBB81A89FB60/RequestType/All/PageNo/1/PageSize/6/Object/null/Category/null/Search/null/Latitude/null/Longitude/null/Radius/null/StartDate/null/EndDate/null
and
Auth-Token = "XXXXXXXXXX"
I want to get JSON from this API using PHP.
Note: I am new in PHP please help.

Use json_decode to convert json data to array.
$url='http://your_url';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'rohit');
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
$resultArray = json_decode($result);
echo "<pre>";
print_r($resultArray);

This can also be done as..
$url='http://your_url';
$result = file_get_contents($url);
$resultData = json_decode($result);
echo "<pre>";
print_r($resultData);

Related

PHP cURL response issue

I'm using the below curl request which returns a XML structured response. The response however seems wrong (first line: string(744978) "<?xml etc). I normally json_decode it, however that does not seem to work. Is this an issue in the endpoint or am I doing something wrong? I would like to convert the response to an array so I can store it in a database.
REQUEST
$url = 'url';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
RESPONSE
use this function for result like
$url = 'url';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$result = api_result($result);
curl_close($ch);
function api_result($result){
$plainXML = $this->mungXML($result);
$arrayResult = json_decode(json_encode(SimpleXML_Load_String($plainXML, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $arrayResult;
}
<!-- begin snippet: js hide: false console: true babel: false -->
function xml_to_json_feed() {
$url = 'http://x';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$xml = simplexml_load_string($result);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
curl_close($ch);
return $array;
}

File Get Contents only gets part of JSON data and not all contents

The code below does not fetch all the JSON content in the URL provided but part of it, I can't find any problem with it:
$json=file_get_contents("https://fantasy.premierleague.com/drf/bootstrap-static");
$data = json_decode($json, true);
var_dump($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://fantasy.premierleague.com/drf/bootstrap static");
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$jdecoded = json_decode($data, true);
curl_close($ch);
print_r($jdecoded);

how to store json data into the database from api in php by curl get?

the code is as below...
$url = 'http://tools.vcommission.com/api/coupons.php?apikey=a7f66abd541c5667ebb8b234f6345df00dbc5dc856d8e7bcd02bb6d636f902fc';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
This is the actual code,,, i need to get the json data and store it in the database.
Use below code
$data = curl_exec($ch);
curl_close($ch);
$json = json_decode($data , true);
then your SQL insert functionality , go through this link http://www.kodingmadesimple.com/2014/12/how-to-insert-json-data-into-mysql-php.html

Getting the title from API

I am trying to get the title of this broadcast from twitch API but I am not sure very much about code.. I have the following code here.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/kraken/channels/test_user1");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
print $result;
and here's what it outputs.
{"mature":null,"status":"Broadcasting LIVE on Justin.tv","broadcaster_language":null,"display_name":"Test_user1","game":null,"language":"en","_id":22747064,"name":"test_user1","created_at":"2011-06-02T20:04:03Z","updated_at":"2016-04-19T21:00:05Z","delay":null,"logo":"https://static-cdn.jtvnw.net/jtv_user_pictures/test_user1-profile_image-ac0a2f0d39dda770-300x300.jpeg","banner":null,"video_banner":null,"background":null,"profile_banner":null,"profile_banner_background_color":null,"partner":false,"url":"https://www.twitch.tv/test_user1","views":99,"followers":1,"_links":{"self":"https://api.twitch.tv/kraken/channels/test_user1","follows":"https://api.twitch.tv/kraken/channels/test_user1/follows","commercial":"https://api.twitch.tv/kraken/channels/test_user1/commercial","stream_key":"https://api.twitch.tv/kraken/channels/test_user1/stream_key","chat":"https://api.twitch.tv/kraken/chat/test_user1","features":"https://api.twitch.tv/kraken/channels/test_user1/features","subscriptions":"https://api.twitch.tv/kraken/channels/test_user1/subscriptions","editors":"https://api.twitch.tv/kraken/channels/test_user1/editors","teams":"https://api.twitch.tv/kraken/channels/test_user1/teams","videos":"https://api.twitch.tv/kraken/channels/test_user1/videos"}}
I am trying to get the portion that says "status":"Broadcasting LIVE on Justin.tv" how can I go about doing so?
$result is encoded in json, you can decode it using json_decode, i.e.:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/kraken/channels/test_user1");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$decodedJson = json_decode($result, true);
echo $decodedJson['status'];
//Broadcasting LIVE on Justin.tv

Curl Script Returning False

I am getting bool(false) when I var_dump $information I can access the API fine via the browser just not via the code below why?
$json = file_get_contents('API URL');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $json);
curl_setopt($ch, CURLOPT_USERPWD, "user:pw");
curl_error($ch);
$result = curl_exec($ch);
curl_close($ch);
$information = json_decode($result, true);
var_dump($result);
This line
$json = file_get_contents('API URL');
should be
$json = 'API_URL';

Categories