I tried very hard to make output of Azuracast json query, but I cant, I'm a php guy and I don't quite get it. Tutorials didn't work either.
I got my token and eveyrthing. I just want to output a album name, song title and artist to string. That's it. I just want to make my little custom player, which is going well except for this. Can you guys check my code and help me out? I know this questions was already answered, but it didnt help for me. It always outputs nothing on my website.
my api link: here
//$url = "https://91.247.70.40/api/nowplaying/2.json";
//$data = file_get_contents($url);
//$infos = json_decode($data, true);
//$nazwa = $infos->cache;
//echo $nazwa;
function jwt_request($token, $post) {
$ch = curl_init('https://91.247.70.40/api/nowplaying/2');
header('Content-Type: application/json');
$post = json_encode($post);
$authorization = "Authorization: Bearer ".$token;
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
echo $result->cache;
you can see i tried two ways. None worked. I've hidden the token. It is set up correctly, i know that.
You seem to be having multiple problems here. The first one is that your API is served on HTTPS but with an invalid certificate / self signed one. file_get_contents and curl will by default fail requests to pages with an untrusted cert. First you should setup a Let's Encrypt certificate as shown in the documentation.
Then your first code will correctly return the decoded JSON data. But you should remove the .json at the end of the URL since this is not a file you are requesting but an API route. There you try to access the decoded data as if it is an object but json_decode($data, true) returns an assoc array so you need to access the data like this $infos['cache']. If you want to get the album name, song title and artist from the data you need to access these fields: $infos['now_playing']['song']['album'], $infos['now_playing']['song']['title'] and $infos['now_playing']['song']['artist'].
Your second code example will not work after you fixed the certificate error because the /api/nowplaying/{station_id} route is a GET route but you define the CURL request as a POST request.
Related
I am trying to access API data from CraftingStore Public API.
When I am trying it on localhost, everything works like it should (1st pic, also printing results for to show).
However, when I am trying to view it on the actual website (2nd pic), it is not working but instead saying to enable cookies and to complete captcha.
When on localhost:
When on website:
Also code for accessing the API:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.craftingstore.net/v7/payments?page=1');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'token:'.$cs_token
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
$dononame = $obj->data[0]->inGameName;
$donobuy = $obj->data[0]->packageName;
What can I do?
This is quite usual for API's with Cloudflare. You'll need to make a page rule for the API domain/URI to disable the Browser Integrity Check.
Source: https://support.cloudflare.com/hc/en-us/articles/200504045-Using-Cloudflare-with-your-API
I am working with rest API. I am using zoho API's for making calls. Working in yii2 I am trying to call a GET API which gives me some details about my project.
/* Set the Request Url (without Parameters) here */
$request_url = 'https://projectsapi.zoho.com/restapi/portal/[PORTALID]/projects/[PROJECTID]/bugs/defaultfields/';
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('authtoken' => 'key')); // here i am using key but in actual i am putting the real key value
$curl_response = curl_exec($ch);
$json = json_decode($curl_response);
var_dump($json);
exit();
Accessing it via POSTMAN when I run this call it gives me NULL in response. I have also tried out the way mentioned in the PHP Example here. But it doesn't work out for me as well.
I must be missing something that I don't know.
Any help would be highly appreciated.
Try checking your headers and make sure you're passing through all the required fields for an example authorization, content type etc.
I am new to APIs and I am having trouble updating the status of issues. I have successfully listed projects and issues but not sure how to update them. I am using PHP to access the API. Can some one please share some example in php to how to update an issue using json. I have read the API documentation but its too minimal. I am not familiar with PUT and I cant find any good example or tutorial on the internet. Thankyou!
This what I tried but didnt work
function update_issue($issue_id){
// set url
$planio_url = "http://something.plan.io/issues/".issue_id.".json?key=185f1edsadfsdafy86578ce82ea70a0eb4267";
// create curl resource
$ch = curl_init();
$data = '{"issue":{"status":{"name":"done","id":21},"notes":"closing all old issues"}}';
$data = json_decode($data);
$ch = curl_init($planio_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
if(!$response) {
return false;
}else{
return $response;
}
}
Returns empty response.
For everybody who wants to copy the snippet above. to update a ticket (like i did)
The URL in $planio_url is missing a $
..issues/".issue_id.".json
must be:
issues/".$issue_id.".json
also i decoded the json with true as second parameter to get an array for http_build_query
works like a charm.
I have been given an example piece of code from a company I'm dealing with for how to post XML data to a URL then read the response. Unfortunately for me this in VBS which I don't have a good working knowledge of:
This is the section of code that I'm interested in. This should pass over the XML file that was read in to oXML then post it and read the response:
set oHTTP = CreateObject("Microsoft.XMLHTTP")
oHTTP.open "POST", "http://www.ophub.net/opxml/response.asp", false,00092,QW 'file url - with dealers Account number, Password
oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oHTTP.setRequestHeader "Content-Length", Len(sRequest)
oHTTP.send oXML
From what I understand of this in PHP this can be done with cUrl and I have come up with the following from bits that I have read online but this doesn't work and I'm not sure why.
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form- urlencoded"));
curl_setopt($ch, CURLOPT_URL, "http://www.ophub.net/opxml/response.asp");
curl_setopt($ch, CURLOPT_USERPWD, "00092:QW");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "XML=" . $xml);
$content=curl_exec($ch);
echo $content;
I'm sure I can't be far off what I need but I can't seem to get there so any hep would be very much appreciated.
In your post fields just set the xml no need to set xml=
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
you also need return transfer true
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
It looks to me like that is precisely what you need. It seems to be an exact translation.
The only two potential issues that I can see are:
You PHP code seems to have a space in the middle of the Content-Type header value, this will most likely break things
You will, if you haven't already done so, need to URL encode the $xml data before sending it as part of a application/x-www-form-urlencoded message (clue's in the type name :-P), which you can do with urlencode().
It might be a good idea to build the body data as a string and echo it out, to ensure that the data is correct:
echo 'XML=' . urlencode($xml);
For the record wrapping XML messages in application/x-www-form-urlencoded is a horrible way to do things. But I've come across more than one API that does it, so I'm going to assume that your code is correct in this regard for the API you are trying to consume.
I am trying to get the details of a venue in Foursquare using the venue ID, but there is something minor that isn't correct:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.foursquare.com/v2/venues/4c599c84f346c9287ff84cca?client_id=[MY_ID]&client_secret=[MY_SECRET]&v=20120609');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
$result = json_decode($contents);
var_dump($result);
curl_close ($ch);
If I change the URL to something like Google News RSS, or one of my XML feed pages, I get results, to the cURL portion is right
If I paste the above URL into my browser (with my actual ID&Secret) then I get a json formatted result with the data that I expect (correct name of venue, etc). So I know the URL is correct.
If I copy the json from the above process and put it into a variable in my code, then set $result to the decode version of that variable then I see the results properly. So I know that the decode/output bit is working.
Somewhere between retrieving the result and storing it in a variable for decoding something is going wrong. I have to assume it is something silly and simple, since all of the parts are there, but I can't figure it out.
Any help is appreciated.
The answer turned out to be that I had to add the following 2 lines:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);