Im need to send a Username and password to api, but y cant connect, i tried in postman and is fine, i need send a json like this
Json
i have this code(sorry is the first time i tried to send data to api)
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://192.168.0.2/apilocation',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"username" : "oswaldo",
"password" : "1"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_setopt($response, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($response, CURLOPT_RETURNTRANSFER, true);
curl_setopt($response, CURLOPT_RETURNTRANSFER, TRUE);
curl_close($curl);
var_dump($response);
But is returning false :(, please help
I think data is should be like this
CURLOPT_POSTFIELDS => json_encode(array("username" => "", "password" => ""))
Also you can try to add this header
'accept: application/json',
maybe a cleaner way
$ch = curl_init("http://192.168.0.2/apilocation");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("username" => "", "password" => "")));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
Related
I'm trying to access to an API that needs and Access Token but I don't know how to implement the token part. This is how I connect to the API. What is wrong in my code?
$ch = curl_init();
$authorization = 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI6Im5aTHhod...';
$headers = array(
'Content-type: application/json',
$authorization,
);
curl_setopt($ch, CURLOPT_URL, "https://graph.microsoft.com/v1.0/me/onenote/notebooks");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$res = curl_exec($ch);
curl_close($ch);
echo $res;
Just a matter of constructing the header, give this a try:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://graph.microsoft.com/v1.0/me/onenote/notebooks',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI6Im5aTHhod'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
CURL Post request yields in error '{"error":"invalid_request"}' (length=27)
here is my code
$url="https://...................";
$authorization=""; //base64 string TE1TQXBpOkxNUContinue...=
$curl = curl_init();
$auth_data = array(
'scope' => 'LMSApi LMSRead',
'grant_type' => 'client_credentials'
);
$ops=array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST =>false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POSTFIELDS=>$auth_data,
CURLOPT_HTTPAUTH=> CURLAUTH_BASIC,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
'Content-Type'=> 'application/x-www-form-urlencode',
'Authorization'=> 'Basic '.$authorization
)
);
curl_setopt_array($curl,$ops);
$response = curl_exec($curl);
var_dump($response);
curl_close($curl);
when using postman it works but returns always false while using above code. don't know what I am doing wrong.curl_error($ch) results in empty string
help is required
if someone have the same problem, following is the solution.
passing 'grant_type=client_credentials&scope=specify scope' directly to CURLOPT_POSTFIELDS instead of an array solved my problem. the code now looks like as bellow.
$url="https://something.com";
$headerpost=array(
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Basic yourbase64string'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials&scope=LMSApi LMSRead');
curl_setopt($ch, CURLOPT_HTTPHEADER,$headerpost);
$result = curl_exec($ch);
$jsonresult=json_decode($result,true);
curl_close($ch);
Trying to use cURL in interfacing a PHP application with Koha, is there a cURL approach on doing this?
Tried the following;
<?php
$url = "http://opac.test.com/api/v1/oauth/token";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "User:Password");
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
echo $data;
curl_close($ch);
?>
I got a Bye response.
This was my solution, to anyone who may encounter the same problem:
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "yourdamain/api/v1/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "grant_type=client_credentials&client_id=yourcliedid&client_secret=yoursecretkey",//these you generate from your koha account
CURLOPT_HTTPHEADER => [
"content-type: application/x-www-form-urlencoded"
],
]);
$response = json_decode(curl_exec($curl));
$err = curl_error($curl);
curl_close($curl);
$token = $response->access_token;
echo "<pre>";
print_r($token);
echo "</pre>";
?>
I'm trying to post a parameter with cURL,
when I tried with this type of format :
CURLOPT_POSTFIELDS => "label=sample"
I exactly got "label" key in the server with "sample" as its value
but I get it empty in the server when I sent it out as a variable .
CURLOPT_POSTFIELDS => "label=$email"
$curl = curl_init();
$user_info=$this->web_model->retriveUserInfo();
$email=$user_info->email;
curl_setopt_array($curl, array(
CURLOPT_URL => "https://test.bitgo.com/api/v2/".$coin."/wallet/".$wallet_id."/address",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "label=$email",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Accept-Encoding: gzip, deflate",
"Authorization: Bearer v2x4e7cf3fb7e6c2e87bf8103e49756b3892b2e350d6cdbaeb65757980",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Content-Length: 11",
"Content-Type: application/x-www-form-urlencoded",
"Host: test.bitgo.com",
"Postman-Token: b3f2ee7c-9a19-479b-bfe2-27000c90e3c7,d611bde9-3eb1-4e2b-b8f5-a7a5f5485726",
"User-Agent: PostmanRuntime/7.15.2",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$response = json_decode($response, true);
$err = curl_error($curl);
curl_close($curl);
my main problem is CURLOPT_POSTFIELDS format for variables !
Doc says about CURLOPT_POSTFIELDS:
This parameter can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with the field name as key and field data as value.
So you can:
Replace: CURLOPT_POSTFIELDS => "label=$email", with CURLOPT_POSTFIELDS => ['label' => $email], and if you'll need more data to pass as POST field you can just add another pair $key => $value to that array or prepare it before setting curl options.
Set POST fields via http_build_query:
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); where $ch is curl handle and $data is array of $key => $value pairs where $key is field name.
But keep in mind:
Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data, while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded.
Many thanks
Finally I had to use Guzzle library simply
$client = new GuzzleHttp\Client(['base_uri' => 'https://test.bitgo.com/api/v2/']);
$response = $client->post($coin.'/wallet/'.$wallet_id.'/address', [
'headers' => [
'Authorization' => 'Bearer v2x4e7cf3fb7e6c2e87bf8103e4975dsddbaeb65ba017b555757980'],
'form_params' => [
"label" => $email
]
very very useful
You can try this
function curl($post = array(), $url, $token = '', $method = "POST", $json = false, $ssl = false){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
if($method == 'POST'){
curl_setopt($ch, CURLOPT_POST, true);
}
if($json == true){
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer '.$token, 'Content-Length: ' . strlen(json_encode($post))));
}else{
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
// Proxy example only
if (!empty($proxy)) {
curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:888');
if (!empty($proxyAuth)) {
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password');
}
}
if($ssl == false){
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
$response = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->statusCode = $statusCode;
if (curl_error($ch)) {
$error = 'CURL_ERROR '.$statusCode.' - '.curl_error($ch);
// print_r('CURL_ERROR '.$statusCode.' - '.curl_error($ch));
throw new Exception('CURL_ERROR '.curl_error($ch), $statusCode);
}
curl_close($ch);
return $response;
}
I would like to ask you how to use PHP request to API with authorized header.
API end point example is
Quotation Service : POST https://api.website.com/v4/quotation
Contenttype: application/json; charset=utf8
Accept: application/json
Authorization: hmac $id:$milliSeconds:$signature
{
"serviceType": "CARD",
"specialRequests": [
"ROUNDTRIP"
]}
as you mention, they use hmac as an authorization in a header and request parameters is serviceType, specialRequest
I try using PHPCurl to make a request but i'm not a good on it. Please give me and advice how to use Curl with this API.
edit:
<?php
$customerId = "585a-SAMPLE-980dfe0097"; //by provider
$privKey = "MC4CAQACBQDSk4ghAgMB---SAMPLE---QIDAOPFAgMAk7QIDAMYq"; //by provider
$requestTime = (int)(microtime(true) * 1000);
$signature = hash_hmac('sha256', $requestTime, $privKey);
$headers = array(
"Authorization: hmac $customerId:$requestTime:$signature"
);
$params = array(
'serviceType' => 'CARD',
'specialRequests' => array('ROUNDTRIP')
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.samplesite.com/v1/quotations",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => $headers,
));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Regards,
Hmm ... maybe something like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.website.com/v4/quotation');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
/* if you want to use SSL certificate, otherwise delete this */
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CAPATH , '/path/to/certificate');
$auth = hash_hmac('sha1', $id.':'.$milisecond.':'.$signature, <ENCRYPTION KEY>, true);
$headers = array(
'Authorization: '.$auth
);
$params = array(
'serviceType' => 'CARD',
'specialRequests' => array('ROUNDTRIP')
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);