I wonder if somebody could help me out with this - I have been trying for a few hours and no joy. When this is submitted I get an error message that says "Use POST method to send XML paramater". I thought I was doing this but obviously not. Any pointers are much appreciated. Obviously the Username, Password and URL are different on the live version
define('XML_PAYLOAD', '<?xml version=\"1.0\" encoding=\"UTF-8\"?><Request><Head><Username>myusername</Username><Password>mypassword</Password><RequestType>HotelSearch</RequestType></Head><Body><HotelIds><HotelId>2234836</HotelId></HotelIds><CheckInDate>2017-05-01</CheckInDate><CheckOutDate>2017-05-08</CheckOutDate><Rooms><Room><NumAdults>2</NumAdults></Room></Rooms><Nationality>GB</Nationality><Currency>GBP</Currency><AvailableOnly>0</AvailableOnly></Body></Request>');
define('XML_POST_URL', 'http://theurl');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
$start = array_sum(explode(' ', microtime()));
$result = curl_exec($ch);
$stop = array_sum(explode(' ', microtime()));
$totalTime = $stop - $start;
if ( curl_errno($ch) ) {
$result = 'ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch($returnCode){
case 404:
$result = 'ERROR -> 404 Not Found';
break;
default:
break;
}
}
curl_close($ch);
echo 'Total time for request: ' . $totalTime . "\n";
echo $result;
exit(0);
Add content type with your request header:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
Or may be text/xml or whatever the server is accepting.
Related
While checking the URL: https://i2.wp.com/jarek-kefir.org/wp-content/uploads/2019/04/pożar-katedry-notre-dame.jpg?ssl=1
I have result status code 400...
This is my function:
public function callAPI($url)
{
$curl = curl_init();
error_log('Checking url: ' . $url);
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
//'APIKEY: 111111111111111111111',
'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
// EXECUTE:
$result = utf8_decode(curl_exec($curl));
$result = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $result);
if (!$result) {
error_log('Connection Failure ' . $result . ' for url: ' . $url);
}
// Check if any error occurred
$error_msg = '';
$error_number = curl_errno($curl);
$error_info = curl_error($curl);
$info = curl_getinfo($curl);
$httpCode = $info['http_code'];
$request_ok = $httpCode == 200 || $httpCode == 201 || $httpCode == 204;
error_log('$httpCode: ' . $httpCode);
if (!$request_ok) {
$info = curl_error($curl);
$error_msg = $info ? $info : "Http code: " . $httpCode;
error_log('Error while checking url: ' . $url . ' : ' . $error_msg);
}
curl_close($curl);
return $error_msg;
}
but when I check it manually or even by curl from command line it works fine.
Probably I miss some configuration for curl here..
heh finally I found out that it is because one polish character.. :)
I am trying to create the proper signature for the Ascentis API. There documentation is http://www.ascentis.com/API/Ascentis_API_Documentation.pdf. Page 4 describes the Signature format.
Here is my PHP code. Am I doing something wrong? I get a "not authorized error".
$url='https://selfservice2.ascentis.com/mycompany/api/v1.1/employees';
$timestamp=gmdate('Y-m-d\TH:i:s\Z');
$path=strtolower(str_replace('https://selfservice2.ascentis.com','',$url));
$signature_string="GET {$path} {$timestamp}";
$signature=hash_hmac("sha1",$signature_string,$secret_key);
$authorization=encodeUrl($client_key).':'.encodeUrl($signature);
Here's a more complete example. Props to #Steve Lloyd for getting me in the right direction.
$codes['secret_key'] = 'my_secret';
$client_key = 'my_key';
$url = 'https://selfservice.ascentis.com/my_company/api/v1.1/employees?lastname=%s';
$timestamp = gmdate('Y-m-d\TH:i:s\Z');
$path = strtolower(str_replace('https://selfservice.ascentis.com', '', $url));
$signature_string = "GET {$path} {$timestamp}";
$signature = base64_encode(hash_hmac("sha1", $signature_string, $codes['secret_key'], TRUE));
$authorization = urlencode($client_key) . ':' . urlencode($signature);
$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, [
"Authorization: " . $authorization,
"Accept: application/xml",
"Timestamp: " . $timestamp,
]);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$data = curl_exec($ch);
$info = curl_getinfo($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "Received $httpcode from $url\n";
echo "Key: $client_key\n";
echo "Signature: $signature\n";
echo "authorization: $authorization\n";
echo "request info:" . var_export($info, TRUE) . "\n";
echo "data:\n";
var_export($data, TRUE)
After playing the trial and error game I was able to get this working. It turns out that you have to set hash_hmac to raw_output. Here is the working code:
$url='https://selfservice2.ascentis.com/mycompany/api/v1.1/employees';
$timestamp=gmdate('Y-m-d\TH:i:s\Z');
$path=strtolower(str_replace('https://selfservice2.ascentis.com','',$url));
$signature_string="GET {$path} {$timestamp}";
$signature=base64_encode(hash_hmac("sha1",$signature_string,$codes['secret_key'],true));
$authorization=encodeUrl($client_key).':'.encodeUrl($signature);
I have a sand box account in fortnox and i am trying to get the access token using following code, but i keep getting same error:
$requestMethod = "GET";
$ch = curl_init();
$options = array(
'Authorization-Code: '. AUTHORIZATION_CODE .'',
'Client-Secret: '. CLIENT_SECRET .'',
'Content-Type: '. CONTENT_TYPE .'',
'Accept: '. ACCEPTS .''
);
curl_setopt ($ch, CURLOPT_CAINFO, "/xampp/htdocs/cacert.pem");
curl_setopt($ch, CURLOPT_URL, "https://api.fortnox.se/3/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, $options);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $requestMethod);
$curlResponse = curl_exec($ch);
$info = curl_getinfo($ch);
//echo 'Took ' . $info['total_time'] . ' seconds for url ' . $info['url'];
echo $curlResponse;
if ($curlResponse === FALSE) {
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
Error: PHP / 4.5.37 Can not sign in , access tokens , or client- secret missing ( 2000311 ) .
I get the Access-Token using the following code which is very close to your code. It worked fine for me.
Your error is described on the Errors page, but it adds nothing new to the text you already shown.
Please note that you can retrive the Access-Token only once, check Authentication section in the Fortnox documentation.
An Access-Token can only be retrieved once with every Authorization-Code, multiple requests with the same Authorization-Code will make both the Authorization-Code and the Access-Token invalid.
public function actionRetrieveAccessToken($authCode, $clientSecret)
{
$headers = [
'Authorization-Code: ' . $authCode,
'Client-Secret: ' . $clientSecret,
'Content-Type: application/json',
'Accept: application/json',
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.fortnox.se/3/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$responseText = curl_exec($curl);
$response = json_decode($responseText, true);
if (isset($response['ErrorInformation'])) {
echo 'Request failed with error messages:' . PHP_EOL;
echo $response['ErrorInformation']['Error'] . ': ';
echo $response['ErrorInformation']['Message'];
echo ' (' . $response['ErrorInformation']['Code'] . ')' . PHP_EOL;
} else {
echo 'Access Token: ' . $response['Authorization']['AccessToken'] . PHP_EOL;
}
}
$content_type = 'application/json';
$api_url="api url here";
$curl = curl_init();
$headers = array(
'Content-Type: '.$content_type.'',
'Authorization : '.'Bearer ' . $this->access_token
);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
if (!empty($data))
{
$data = json_encode($data);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
$response = curl_exec($curl);
logthis($response,"utils");
curl_close($curl);
My app was posting on facebook without user interaction in the last 2-3 days, but now, and I don't why, this is not working.
Here's my code:
function post_fb($fb_text) {
echo file_get_contents("https://graph.facebook.com/me/feed?access_token="."***".$fb_text);
$ch = curl_init();
$data = array('message' => $fb_text, 'access_token' => "***");
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/me/feed');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$tuData = curl_exec($ch);
if(!curl_errno($ch)){
return true;
/*$info = curl_getinfo($ch);
print_r($info);
echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];*/
}
/*else {
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
echo $tuData;*/
}
I have been working with Googgle's Custom Search Engine API to programmatically add and delete annotations. I feel like I have followed their documentation and others tips very closely. I can't seem avoiding a 'bad request' HTTP 400 error. My code is below
$url = "http://www.google.com/cse/api/default/annotations/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array(
"Authorization: GoogleLogin auth='DQAAAMI......n9dGUyA'",
"Content-Type: text/xml"
));
curl_setopt($ch, CURLOPT_POST, 1);
$data = '<?xml version="1.0"?>'.chr(10);
$data .= '<Batch>'.chr(10);
$data .= chr(9).'<Add>'.chr(10);
$data .= chr(9). chr(9).'<Annotations>'.chr(10);
$data .= chr(9). chr(9). chr(9).'<Annotation about=\"\">'.chr(10);
$data .= chr(9).chr(9). chr(9). chr(9).'<Label name=\"my_engine\"/>'.chr(10);
$data .= chr(9). chr(9). chr(9).'</Annotation>'.chr(10);
$data .= chr(9). chr(9).'</Annotations>'.chr(10);
$data .= chr(9).'</Add>'.chr(10);
$data .= '</Batch>'.chr(10);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if ( curl_errno($ch) ) {
$result = 'cURL ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $returnCode."<br/>";
switch($returnCode){
case 200:
break;
default:
$result = 'HTTP ERROR -> ' . $returnCode;
break;
}
}
curl_close($ch);
echo $result;
if anybody can help me or at least steer me in the right direction I would be very grateful
Ok, firstly, you'll need to encode the xml data thats sent to google. Secondly you should get rid of all the tabs and formatting, the receiving engine is not concerned with that.
So try this:
$data = '<?xml version="1.0"?><Batch><Add><Annotations><Annotation about=\"\"><Label name=\"my_engine\"/></Annotation></Annotations></Add></Batch>';
$data = urlencode($data); // encode the string to send via http
I think that should get you going. You may find other errors but this should clear the 400 error you are getting.