Php: spotify playback track using PUT curl - php

In the Spotify documentation, it indicates that it is possible to send the instructions to play the song.
Documentation here
He tried to transfer this to php but did not respond to the request.
I don't know what I will do wrong, I hope to solve this problem.
This my request body
{
"uris": [
"spotify:track:7fODjB7BrQTGqh0hogW6XD"
]
}
From the web it works, but in php I can't send the request.
{ "error": { "status": 401, "message": "Invalid access token" } }
This is my php code [UPDATED - 2019/11/8]
function sendTrack($token){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/v1/me/player/play' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['uris' => ['spotify:track:7fODjB7BrQTGqh0hogW6XD']]));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$token.''));
$result=curl_exec($ch);
echo $result;
}

Your CURL_POSTFIELDS are in wrong format. Try this
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['uris' => ['spotify:track:7fODjB7BrQTGqh0hogW6XD']]);
Edit:
Also according to spotify documentation you need to send a PUT request so instead of
curl_setopt($ch, CURLOPT_POST, 1 );
try
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
Edit2:
just tried this code and it works, only creating error that no active device was found which im sure happened because i missed a step or two.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/v1/me/player/play' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['uris' => ['spotify:track:7fODjB7BrQTGqh0hogW6XD']]));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: json', 'Authorization: Bearer ' . $token));
$result=curl_exec($ch);
echo $result;

Related

Accessing the Aleph API with PHP, always getting error "No schema is specified for the query."

I'm trying to access the OCCRP Aleph API
https://redocly.github.io/redoc/?url=https://aleph.occrp.org/api/openapi.json
with the following PHP program
$url = 'https://aleph.occrp.org/api/2/entities?q="title:bank"';
$ch = curl_init();
$headeroptions = array('Accept: application/json', 'Content-Type: application/json', 'Authorization: ApiKey 363af1e2b03b41c6b3adc604956e2f66');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headeroptions);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$array = json_decode($data, true);
Var_dump($array);
I always get the error "No schema is specified for the query."
When I call Statistics, I get a correct answer...
after inspecting how the API works in their own website, I've notice you're missing a required parameter, which indicates the schema. Just add this to your url:
&filter:schemata=Thing
and it should work

Instagram login error : Invalid Client Secret

If I am trying to login with Instagram using api,
Code :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiHost);
curl_setopt($ch, CURLOPT_POST, count($apiData));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonData = curl_exec($ch);
curl_close($ch);
Error :
{
error_type: "OAuthException",
code: 400,
error_message: "Invalid Client Secret"
}
Does any one have idea?
As mentioned in the comment: check if there is white space when you copied it.
But in my case removing the white space still didn't fix the error. Instead I tried writing it directly and that seemed to do the trick.

cURL HTTP Request with PHP (Routific API)

I'm trying to make an HTTP Request in PHP using cURL. The Request goes to the Routific (Route Optimization) API -> https://docs.routific.com/v1.3.1/docs
The idea is to send a JSON with different waypoints and get an optimal route back. Unfortunately, I get the following error when executing the code:
{"error":"Missing network"}
My Code looks as follows:
<?php
$data_string = json_encode(file_get_contents('Routes.json'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.routific.com/v1/vrp");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: bearer API_KEY')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$result = curl_exec($ch);
echo $result;
curl_close($ch);
?>
What is wrong?
Thanks!

Twitch API - follow a user, getting "411 Length Required" error

So i'm writing a function for a client whom wants a simple function to use on his social site so that users can follow channel on Twitch, no SDKs nothing like that i have the following function:
function twitch_follow_channel($user, $channel, $client_id, $access_token) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.twitch.tv/kraken/users/'.$user.'/follows/channels/'.$channel.'?oauth_token='.$access_token);
$h = 'Client-ID: '.$client_id.', Accept: application/vnd.twitchtv.v3+json, Authorization: OAuth '. $access_token;
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth '.$access_token,
'Client-ID: '.$client_id,
'Content-Length: '.strlen($h),
'Accept: application/vnd.twitchtv.v3+json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
$r = curl_exec($ch);
$ci = curl_getinfo($ch);
r($ci);
r($r);
return json_decode($r, true);
}
i include the Content-length in the HTTP HEADER i don't know what i'm missing
Notes
The access token has user_follows_edit scope.
r() is used instead of var_dump()
I'm already aware of the DOCs at GitHub, followed it carefully
Recently did that myself so how about you add:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
This way you say CURL to use PUT as request.
To unfollow simply replace PUT with DELETE and your gucci.
Atom8tik

Not response and not error using cURL

I'm working with cURL but i don't have any response and any error when I'm using it. I have looking for any answer on the web but i haven't found anything. Too appear "Resource id #478"
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ep);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, "data=$json");
curl_setopt($ch, CURLOPT_USERPWD, "$this->username:$this->password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$response = curl_exec($ch);
curl_close($ch);
$json is an array and $ep is the url of the API with I'm working on.
When I use "echo $response", appear "Authorization Required"
So, the question is why don't I have any response and any error? Is the authentication maybe?
Thanks for any answer

Categories