I currently want to make a plugin for Woocommerce for some personal stuff I was trying to connect through the API but I get:
{"code":"woocommerce_rest_authentication_error","message":"Invalid
signature - provided signature does not match.","data":{"status":401}}
Would anyone know where the signature isn't valid?
my curl request:
$curl = curl_init();
$cnonce = hash('sha256', makeRandomString());
$requesturl = "http://my-marketplace.test/wp-json/wc/v2/products";
$timestamp = time();
$signature = createSignature($requesturl,$consumerKey, $consumerSecret, $timestamp, $cnonce);
echo $signature;
curl_setopt_array($curl, array(
CURLOPT_URL => $requesturl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"authorization: OAuth oauth_consumer_key=\"".$consumerKey."\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"".$timestamp."\",oauth_nonce=\"".$cnonce."\",oauth_signature=\"".$signature,
"cache-control: no-cache",
"content-type: multipart/form-data",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
And my createSignature() function:
function createSignature($myurl, $consumerKey, $consumerSecret, $timestamp, $cnonce){
$sig = "GET&".rawurlencode($myurl)."&";
$sig = $sig.rawurlencode("oauth_consumer_key=".$consumerKey."&
oauth_signature_method=HMAC-SHA1&
oauth_timestamp=".$timestamp."&
oauth_nonce=".$cnonce);
return hash_hmac ( "sha1",$sig, $consumerSecret);
}
Related
The cURL request works in Postman with the following:
curl -i -u "apikey:12345" \
-F training_data=#rtcu.csv \
-F training_metadata="{\"language\":\"en\",\"name\":\"RTCU\"}" \
"https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/v1/classifiers"
Postman's generated cURL code for PHP returns { "code" : 400, "error" : "Data too small", "description" : "The number of training entries received = 0, which is smaller than the required minimum of 5" }
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"training_data\"; filename=\"rtcu.csv\"\r\nContent-Type: text/csv\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"training_metadata\"\r\n\r\n{\"language\":\"en\",\"name\":\"RTCU\"}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic 12345",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
I've tried adding the "#" in front of the filename like suggested in other posts with no success. I haven't had this problem with other IBM Watson services and their cURL calls. What could be the issue?
Using CURLFile worked for me
$uploadFilePath = 'rtcu.csv';
$name = "RTCU";
$lang = "en";
$uploadFileMimeType = "text/csv";
$uploadFilePostKey = 'training_data';
$metaPostKey = "training_metadata";
$uploadFile = new CURLFile(
$uploadFilePath,
$uploadFileMimeType,
$uploadFilePostKey
);
$curlHandler = curl_init();
curl_setopt_array( $curlHandler, [
CURLOPT_URL => 'https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic 12345",
"cache-control: no-cache"
),
CURLOPT_POSTFIELDS => [
$uploadFilePostKey => $uploadFile,
$metaPostKey => "{\"language\":\"{$lang}\",\"name\":\"{$name}\"}"
],
] );
$response = curl_exec( $curlHandler );
$err = curl_error( curlHandler );
curl_close( curlHandler );
if ( $err ) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}
I want to send email using Sendinblue API in php.
I added some php variable in the email code, but the script is not working!
<?php
$email = "12345#gmail.com";
$username = "12345";
$html = "<h1>Hi!</h1>";
$subject = "Hello";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendinblue.com/v3/smtp/email",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"sender\":{\"name\":\"ABC\",\"email\":\"abc#abc.com\"},\"to\":[{\"email\":\".$email.\",\"name\":\".$username.\"}],\"bcc\":[{\"email\":\"admin#abc.com\",\"name\":\"Admin\"}],\"htmlContent\":\".$html.\",\"subject\":\".$subject.\"\"replyTo\":{\"email\":\"support#abc.com\",\"name\":\"Support\"}}",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"api-key: API-KEY", //hidden because of privacy reason
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
However, I receive this error message:
{"error":{"status":400,"message":"Input must be a valid JSON object","code":"bad_request"}}
Update
$email = "12345#gmail.com";
$username = "12345";
$html = "<h1>Hi!</h1>";
$subject = "Hello";
with
$arr = [
'email'=>"12345#gmail.com",
'username'=>"12345",
'html'=>"<h1>Hi!</h1>",
'subject'=>"Hello",
];
$postData = json_encode( $arr );
Put encode string in curl
CURLOPT_POSTFIELDS => $postData
This question already has answers here:
Can PHP cURL retrieve response headers AND body in a single request?
(16 answers)
Closed 3 years ago.
I'm trying to get a Token in the header of an POST request. With Postman it works but when I run this in PHP I get the right content but cannot extract the header information.
I have copied the PHP code generated in Postman but I cannot extract the header information. I only get the content.
The code I used:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://xxxx.yyyy.xy/xxxxxx/xxxxxxxxxxx",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"Content-Type: application/x-www-form-urlencoded",
"Postman-Token: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
The header should include "Token".
I cannot recieve any header information.
I found a solution on the problem:
<?php
$curl = curl_init();
$Username = "XXXXXXXXX";
$Password = "YYYYYYYYYY";
$Credentials = base64_encode($Username.':'.$Password);
$Credentials_Auth = "Authorization: Basic ".$Credentials;
// echo "<br>Cred-auth: ".$Credentials_Auth."<br>";
curl_setopt_array($curl, array(
CURLOPT_URL => "https://XXXXXXXXX/YYYYYYYYYYY/ZZZZZZZZZZZ",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
**CURLOPT_HEADER => 1,**
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
$Credentials_Auth,
"Content-Type: application/x-www-form-urlencoded",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) { echo "cURL Error #:" . $err;}
else { echo $response; }
**$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $header_size);
$body = substr($response, $header_size);**
curl_close($curl);
**header("Content-Type:text/plain; charset=UTF-8");
echo $headers;
echo $body;**
?>
I'm working with an API which is made by a classmate. I used Postman to generate the PHP cURL for the connection, with Authorization Basic. This works perfectly. Now, I want to get rid of the Authorization Basic and use my own API Key & Secret (password). Are there any good ways to do this?
Thanks in advance!
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "Some link here",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic //something here",
"cache-control: no-cache",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$result = json_decode($response,true);
}
I have tried below code in PHP to get the defect details from ALM but its not showing any response in browser. But the same is working in POSTMAN . Can someone help me here
Here is the document of REST API USAGE REST DOCUMENT FROM ALM
I have already tried existing posts from Stackoverflow
HP ALM REST API login using PHP CURL
ALM REST API v12.50 error 401
Nothing is helping so posted a new question
Note : Due to security purpose header value is kept as encoded value
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://hostname/qcbin/api/domains/domainname/projects/projectname/defects/?limit=10",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic encoded value",
"cache-control: no-cache",
"postman-token: a8a2398d-7a0a-0ebd-a586-58a40e524a9a"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
I have finally found the solution and below is the approach
First we need get to LWSSO_COOKIE_KEY,QCSession,ALM_USER,XSRF_TOKEN values from the ALM Authentication link then we should use the values for subsequent calls
Below is the complete working code to get the list of defects by entering ALM Credentials
<?php
$curl = curl_init();
Header('Content-type: application/json');
$credentials = "username:password";
curl_setopt_array($curl, array(
CURLOPT_URL => "https://host:port/qcbin/api/authentication/sign-in",
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic " . base64_encode($credentials) ,
"cache-control: no-cache"
) ,
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err)
{
echo "cURL Error #:" . $err;
}
else
{
// If there is no error then get the response to form the array of headers to get the different values required
$array_start = explode(';', $response);
foreach ($array_start as $key => $value) {
$remove_from_string = ['HTTP/1.1 200 OK','Path=/','HTTPOnly','HttpOnly','Content-Length',': 0'];
$replace_array = ['','','','','',''];
$value = str_replace($remove_from_string,$replace_array,$value);
$value = trim(preg_replace(('/Expires: [a-zA-Z]+, [0-9]+ [a-zA-Z]+ [0-9]+ [0-9]+:[0-9]+:[0-9]+ [a-zA-Z]+/'), '', $value));
$value = trim(preg_replace(('/Server: [a-zA-Z0-9.\(\)]+/'),'',$value));
if (!empty($value)) {
$almheaders[trim(explode('=',$value)[0])] = explode('=',$value)[1];
}
}
$LWSSO_COOKIE_KEY = $almheaders['Set-Cookie: LWSSO_COOKIE_KEY'];
$QCSession = $almheaders['Set-Cookie: QCSession'];
$ALM_USER = $almheaders['Set-Cookie: ALM_USER'];
$XSRF_TOKEN = $almheaders['Set-Cookie: XSRF-TOKEN'];
// Now form the Cookie value from the above values.
$cookie = "Cookie: JSESSIONID=33eyr1y736486zcnl0vtmo12;XSRF-TOKEN=$XSRF_TOKEN;QCSession=$QCSession;ALM_USER=$ALM_USER;LWSSO_COOKIE_KEY=$LWSSO_COOKIE_KEY";
// echo $cookie;
$curl = curl_init();
Header('Content-type: application/json');
curl_setopt_array($curl, array(
CURLOPT_URL => "https://host:port/qcbin/api/domains/CET_NTD/projects/BILLING_OPERATIONS/defects",
// CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic " . base64_encode($credentials) ,
"cache-control: no-cache",
"Accept: application/json",
$cookie
) ,
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err)
{
echo "cURL Error #:" . $err;
}
else
{
echo $response;
}
}
?>