Forum Discussion

RobertNTalbert's avatar
RobertNTalbert
Qrew Member
5 years ago

PHP Code for API XML Post

I have an API_Authenticate and API_DoQuery calls working in Postman, but my PHP knowledge is 20 years old. The following code results in an http 301 error.  I am lost as to how construct the contents of my $request-xml variable.  I am out of my depth with the curl code.  I may have some other problems:) Could someone kindly respond with a correction(s):

<!DOCTYPE html>
<html>
<body>

<?php
$request_xml = "<?xml version='1.0' encoding='utf-8'?>
Content-Type: application/xml
QUICKBASE-ACTION: API_Authenticate
<qdbapi>
<username>MyUserName</username>
<password>MyPassword</password>
<hours>24</hours>
</qdbapi>";

//Initialize handle and set options

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://mydomain.quickbase.com/db/main');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));

//Execute the request
$result = curl_exec($ch);

//Check for errors
if ( curl_errno($ch) ) {
$result = 'ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch($returnCode){
case 200:
break;
default:
$result = 'HTTP ERROR -> ' . $returnCode;
break;
}
}

//Close the handle
curl_close($ch);

//Output the results
echo $result;
echo "<br>";
echo "ticket = " . $response->ticket;
?>

</body>
</html>

------------------------------
Rob Talbert
------------------------------