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);
Related
Am trying to implement ibm watson language translator using this IBM curl request data below
url -X POST -u "apikey:{apikey}" --header "Content-Type: application/json" --data "{\"text\": [\"Hello, world! \", \"How are you?\"], \"model_id\":\"en-es\"}" "{url}/v3/translate?version=2018-05-01"
I have written two codes in an attempt to get it work but when I run both codes below. it displays error
{"code":401, "error": "Unauthorized"}{"code":401, "error": "Unauthorized"}
please what can I do to get it to work. Thanks
first code attempt
<?php
$apikey ="my-api-key-goes-here";
$url = "https://api.eu-gb.language-translator.watson.cloud.ibm.com/instances/48bed10c-ce07-4a77-adec-014e0729de40/v3/translate?version=2018-05-01";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'apikey'=>$apikey
]));
//$params_post ="{\"text\": [\"Hello, world! \", \"How are you?\"], \"model_id\":\"en-es\"}";
$params_post ='{"text":["Hello"],"model_id":"en-es"}';
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch,CURLOPT_POSTFIELDS, $params_post);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
echo $response = curl_exec($ch);
curl_close($ch);
print_r($response);
?>
second code attempt
<?php
$apikey ="my-api-key-goes-here";
//$params_post ="{\"text\": [\"Hello, world! \", \"How are you?\"], \"model_id\":\"en-es\"}";
$params_post ='{"text":["Hello"],"model_id":"en-es"}';
$uname ="apikey";
$pass =$apikey;
/*
$header = array(
'Content-Type: application/json',
"Authorization: apikey:$apikey"
);
*/
$header = array(
'Content-Type:application/json',
'Authorization: Basic '.$apikey
);
// Set options for REST call via curl
$endpointurl ="https://api.eu-gb.language-translator.watson.cloud.ibm.com/instances/48bed10c-ce07-4a77-adec-014e0729de40/v3/translate?version=2018-05-01";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $endpointurl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, "$uname:$pass");
//curl_setopt($ch, CURLOPT_USERPWD, $uname . ":" . $pass);
//curl_setopt($curl, CURLOPT_USERPWD, 'apikey:' . $apikey);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl,CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($curl,CURLOPT_POSTFIELDS, $params_post);
echo $result = curl_exec($curl);
print_r($result);
?>
I know what this topic is old, but here is what helped me.
$url = "{URL here}/v3/translate? version = 2018-05-01";
$user = "apikey";
$pass = "{API key here}";
$options = array (
CURLOPT_RETURNTRANSFER =>true,
CURLOPT_FOLLOWLOCATION =>true,
CURLOPT_AUTOREFERER =>true,
);
$data = [
'text' =>[
'Hello, world!',
'How are you?'
],
'model_id' =>'en-es'
];
$ch = curl_init ();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERPWD, $user. ":". $pass);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json'));
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt ($ch, CURLOPT_VERBOSE, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, json_encode ($data));
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt_array ($ch, $options);
$result = curl_exec ($ch);
curl_close ($ch);
And here is where I found this
https://www.tutorialfor.com/questions-111457.htm
I hope it is helpful to someone.
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);
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);
I'm trying to generate a token with the "Cloud Identity" API from Rackspace(https://developer.rackspace.com/docs/cloud-identity/v2/developer-guide/#generate-an-authentication-token)
This is the request i need:
$ curl https://identity.api.rackspacecloud.com/v2.0/tokens \
-X POST \
-d '{"auth":{"RAX-KSKEY:apiKeyCredentials":{"username":"yourUserName","apiKey":"yourPassword"}}}' \
-H "Content-type: application/json" | python -m json.tool
And this is how I'm trying to do it:
<?php
$url = 'https://identity.api.rackspacecloud.com/v2.0/tokens';
$apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$data = "'{\"auth\":{\"RAX-KSKEY:apiKeyCredentials\":{\"username\":\"XXXXXXXX\",\"apiKey\":\"$apiKey\"}}}'";
$headers = array();
$headers[0] = '"Content-Type: application/json" | python -m json.tool';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
$response = curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo($info);
?>
And I get this: 415 {"unsupportedMediaType":{"code":415}}
When I change the header from
$headers[0] = '"Content-Type: application/json" | python -m json.tool';
to
$headers[0] = 'Content-Type: application/json | python -m json.tool';
I get this
And finally when I change the header to this one:
$headers[0] = 'Content-Type: application/json';
i get this error code: 400 {"badRequest":{"code":400,"message":"Invalid json request body"}}
Am I doing it right?
Thanks in advance.
Is there any reason why you're not using the PHP SDK? It makes doing things like this a lot easier.
You'd need to change your PHP to:
<?php
$url = 'https://identity.api.rackspacecloud.com/v2.0/tokens';
$username = 'foo';
$apiKey = 'bar';
$data = <<<EOT
{"auth": {"RAX-KSKEY:apiKeyCredentials":{"username":"$username","apiKey":"$apiKey"}}}
EOT;
$headers = array('Content-Type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
$response = curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE);