I'm trying to get in PHP this curl:
curl -X POST -u "apikey:MY_API_KEY" \
--header "Content-Type: text/plain;charset=utf-8" \
--header "Accept: application/json" \
--data-binary "MY_TEXT" \
"https://MY_DIRECTION"
so far I came up with this:
$curl = curl_init();
$post_args = array('data-binary' => $MY_TEXT );
$header_args = array(
'Content-Type: text/plain;charset=utf-8',
'Accept: application/json'
);
$apiKey = '$MY_API_KEY';
$api_args = array('apikey: ' . $apiKey);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $api_args);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_args);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header_args);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_URL, $MY_DIRECTION);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
json_decode($result, true);
I'm trying to use Personality Insights service of IBM.
Let's try with this-
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://MY_DIRECTION');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "MY_TEXT");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . 'MY_API_KEY');
$headers = array();
$headers[] = 'Content-Type: text/plain;charset=utf-8';
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Related
What would be the php curl version of the below command
curl -X PUT -H 'Content-Type: text/csv' -H 'Accept: application/json' -d #file.csv [URL]
I have the below php curl version which doesn't work
$headers = [
'Content-Type: text/csv',
'Accept: application/json'
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filename));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
Try below code
$headers = [
'Content-Type: text/csv',
'Accept: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ''.$url'');
curl_setopt($ch, CURLOPT_PUT, 1);
$fp = fopen($file_path, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response= curl_exec ($ch);
fclose($fp);
There is a tool to convert cURL command to PHP version of cURL. You can try this website: curl-toPHP.
This is the example output using your cURL command.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'your-post-remote');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post = array(
'file' => '#' .realpath('file.csv')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
$headers = array();
$headers[] = 'Content-Type: text/csv';
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
I have some trouble with translate bash curl to PHP. Look's it is not working for me. I don't know how to check it is correct code.
curl -X "POST" "https://someurl.com/oauth/token" \
-H "Authorization: Basic eyJ...V6w" \
--data-urlencode "grant_type=password" \
--data-urlencode "scope=api" \
--data-urlencode "username=user#example.uri" \
--data-urlencode "password=p...d"
I have tryied with that:
$query1 = urlencode("grant_type=password");
$query2 = urlencode("scope=api");
$query3 = urlencode("username=login#mail.com");
$query4 = urlencode("password=loginpass");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://someurl.com/oauth/token&".$query1."&".$query2."&".$query3."&".$query4."");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Authorization: Basic ZnVydGFzdGljLTZhMTAzNTE4Y2YyOGNhNmI3OTNhYzljNmJjM";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
echo "</pre>";
print_r($result);
echo "</pre>";
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
You can use this way. Keep in mind that you are posting the parameters. So the query string should not be attached to the URL but sent as post parameters
$data = array(
'grant_type' => 'password',
'scope' => 'api',
'username' => 'user#example.uri',
'password' => 'p...d'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://someurl.com/oauth/token");
$headers = array();
$headers[] = "Authorization: Basic ZnVydGFzdGljLTZhMTAzNTE4Y2YyOGNhNmI3OTNhYzljNmJjM";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
http_build_query($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
echo $response = curl_exec($ch);
I am trying to add a track to my own playlist with a little php script, but it won't work.
I always get the errror:
{ "error" : { "status" : 400, "message" : "Error parsing JSON." } }
This is the spotify document for adding tracks:
https://developer.spotify.com/web-api/add-tracks-to-playlist/
Has anybody an idea what the problem could be?
$token='my Access token';
$headers = array(
"Accept: application/json",
"Authorization: Bearer " . $token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/v1/users/*myusername*/playlists/*myplaylistID*/tracks' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, 'uris=spotify:track:3DXncPQOG4VBw3QHh3S817' );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result=curl_exec($ch);
print "$result";
$result = json_decode($result, true);
curl_close($ch);
I solved it finally:
$key='***AccessKey***';
$url = 'https://api.spotify.com/v1/users/USERID/playlists/playlistID/tracks?uris=spotify%3Atrack%3A3DXncPQOG4VBw3QHh3S817';
$headers = array(
"Content-Length: 0",
"Accept-Encoding: gzip, deflate, compress",
"User-Agent: runscope/0.1",
"Content-Type: application/json",
"Authorization: Bearer ".$key);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
print_r($response);
Try this:
$data = array('uris' => 'spotify:track:3DXncPQOG4VBw3QHh3S817');
$data_json = json_encode($data);
// ...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/...');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array
(
'Content-Type: application/json',
'Content-Length: '.strlen($data_json),
'Authorization: Bearer '.$token
));
$result = curl_exec($ch);
// ...
i would like to make a CURL to get an auth tokken.
The platform dev forum give me that:
curl -X POST \
--header 'Content-Type: application/json; charset=utf-8' \
--header 'Accept: application/json' \
-d '{"email":"MY_EMAIL","password":"MY_PASSWORD"}' \
'https://api.voluum.com/auth/session'
How do i make that work in PHP?
Try this: https://incarnate.github.io/curl-to-php/
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.voluum.com/auth/session");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"email\":\"MY_EMAIL\",\"password\":\"MY_PASSWORD\"}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json; charset=utf-8";
$headers[] = "Accept: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
$vars = '{"email":"MY_EMAIL","password":"MY_PASSWORD"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.voluum.com/auth/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = ['Content-Type: application/json; charset=utf-8',
'Accept: application/json'];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
exit;
}
curl_close ($ch);
print_r($server_output);
You can do it like below:-
<?php
$data_string = '{"email":"MY_EMAIL","password":"MY_PASSWORD"}';
$ch = curl_init('https://api.voluum.com/auth/session');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Accept: application/json'
));
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
exit;
}
curl_close ($ch);
var_dump($result);
I run it and found below response (because i don't have mail-id and password):- https://prnt.sc/gdz82r
But the pleasant part is code executed successfully and when you will provide right credentials then it will give you correct output.
I am trying to convert this cURL command line call to a PHP script. I've read many articles on it , and have tried several options but none were able to return a result. Here's the command line call:
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp
:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" \
-d "grant_type=client_credentials"
Here is what I have tried so far:
$data = "grant_type=client_credentials";
$url = "https://api.sandbox.paypal.com/v1/oauth2/token";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch,CURLOPT_HTTPHEADER, array("Accept:application/json","Accept- Language:en_US"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "[$idclient]:[$sekret]");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
var_dump($result);
Please help me in converting it to a PHP script call.
thank you
$url = 'https://api.sandbox.paypal.com/v1/oauth2/token';
$header_array = array(
'Accept: application/json',
'Accept-Language: en_US',
);
$username = 'EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp';
$password = 'EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp';
$data_string = 'grant_type=client_credentials';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);