0

I'm new to PHP, and I am trying to create this CURL post that moves given xml data to a given url api restful Web Service. I am able to make the xmlhttprequest POST work with HURL.it using the same information on the code below. The script gets to their server, but it returns with a 422 error "Your request could not be processed". Am I missing something?

<?php
define('XML_PAYLOAD', '<subscriptions><opt_in>invite</opt_in><user><mobile_phone>5555555555</mobile_phone><attribute_paths>ar/adona/3day</attribute_paths></user></subscriptions>');
define('XML_POST_URL', 'http://vibescm.com/api/subscription_campaigns/555555/multi_subscriptions.xml');


$username = 'username';
$password = 'password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additionalHeaders));
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ; 
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password );
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'));

/**
 * Execute the request and also time the transaction
 */
$start = array_sum(explode(' ', microtime()));
$result = curl_exec($ch);
$stop = array_sum(explode(' ', microtime()));
$totalTime = $stop - $start;

/**
 * Check for errors
 */
if ( curl_errno($ch) ) {
    $result = 'cURL 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 and time
 */
echo 'Total time for request: ' . $totalTime . "\n";
echo $result;  

/**
 * Exit the script
 */
exit(0);
?>

CORRECTED

<?php
/**
 * Define POST URL and also payload
 */
define('XML_PAYLOAD', '<XML></XML>');
define('XML_POST_URL', 'WEBSITE-ADDRESS');

/**
 * Initialize handle and set options
 */
$username = 'USERNAME';
$password = 'PASWORD';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ; 
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD);
curl_setopt($ch, CURLOPT_VERBOSE, true);


/**
 * Execute the request and also time the transaction
 */
$start = array_sum(explode(' ', microtime()));
$result = curl_exec($ch);
$stop = array_sum(explode(' ', microtime()));
$totalTime = $stop - $start;

/**
 * Check for errors
 */
if ( curl_errno($ch) ) {
    $result = 'cURL 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 and time
 */
echo 'Total time for request: ' . $totalTime . "\n";
echo $result;  

/**
 * Exit the script
 */
exit(0);
?>
Joseph Casey
  • 1,283
  • 1
  • 14
  • 34
  • 1
    You didn't set `curl_setopt($ch, CURLOPT_POST, true);` – user4035 Jun 12 '14 at 18:05
  • 3
    You set `CURLOPT_HTTPHEADER` option twice and the 2nd value overwrite the 1st. You have to merge these values into one array. Although you don't need set `Connection: close` header since curl will set this itself. – hindmost Jun 12 '14 at 18:05
  • 1
    Your question is missing decent information from your debugging. You need to contact the Server Owner of the Endpoint to learn about the specific meaning of the 422 Status Code (4xx just means error, so you made an error, but we can not say which one) and/or their support how they report error details. If more error information on the HTTP level is available (e.g. in form of response headers) and/or you want to learn about which request is crafted by curl, please see: [Php - Debugging Curl](http://stackoverflow.com/a/14436877/367456) – hakre Jun 12 '14 at 18:21
  • You're right hakre . I am getting in touch with their developers now. Their system only has 3 error codes, so its a bit of a guessing game. @hindmost's noticing the double httpheader made the error codes stop appearing, but I'm still not getting a success sms. Thanks for the help guys. – Joseph Casey Jun 12 '14 at 18:38

0 Answers0