I am writing a program that uses cURL to access an UPS API. The problem I am running into is that I keep getting return of bool(false) doing a var_dump on curl_exec();

Can you see if I am passing the information correctly using POST?

I've done a few other API's w/ UPS but this is the first time I've tried using POST w/ TLS1.2. For TLS1.2 I have the CURL_SSLVERSION, 6.

I appreciate any insight. Thanks.

<?php
error_reporting(E_ALL);
ini_set('display_errors','1');

//credentials
$accessKey = "abc123";
$user = "abc";
$pass = "123";
$targetDIR = '/abc/123/xyz';
$port = '443';
$url = 'https://www.pld-certify.ups.com'.$targetDIR.'';

$postFields = array('AppVersion' => '1.0', 
					'AcceptUPSLicenseAgreement' => 'Yes',
					'ResponseType' => 'application/x-ups-pld',
					'VersionNumber' => 'V4R1',
					'UserId' => $user,
					'Password' => $pass
				   );
//create URL out of post
$fieldString = NULL;
foreach($postFields as $a=>$b){
	$fieldString .= $a . '=' . $b . '&';
}
$fieldString = rtrim($fieldString, '&');


$body = '--BOUNDARY
        Content-type: application/x-ups-binary
        Content-length: 719

    020094                        2011R0    2014093000000012939600400909000000001*AA111111    US0               0129396004000001*BA1Z1111110300002095                 00001+0000000000000070 +0000000000000000LBS03PRE10                                   3INUSD000001*CA18TEST ORDER                         TEST ORDER                         10800 E 45TH AVE                                                                                         DENVER                        CO   80239    US15555555555                                             *PA1Z1111110300002095                 02+0000070                                                                                      +0000000+00000000+00000000+00000000*SA000004

    --BOUNDARY--';


$header[] = "Host: www.pld-certify.ups.com";
$header[] = "MIME-Version: 1.0";
$header[] = "Content-type: multipart/mixed; boundary=BOUNDARY";
$header[] = "Content-length: ".strlen($body);
$header[] = "Cache-Control: no-cache";
$header[] = "Connection: close \r\n";
$header[] = $body;


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($postFields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

$result = curl_exec($ch);
curl_close($ch);

var_dump($result);
?>

    I added curl_errno(); & curl_error(); and got the following error:

    60
    SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

    Looking into that I added

    curl_setopt($ch, CURLOPT_CAINFO, "path/to/cacert.pem");
    

    And it appears I am getting through to UPS but hitting some other type of error w/ my data structure.

    Will mark this resolved. Thanks.

      I can never remember if/when you need to urlencode() values being added to the cURL POST data, in case that has anything to do with your remaining issues. 🙂

        Thanks for the reply... I tried urlencoding and got the same error response.

        Has anyone tried Guzzle? Found it doing a search but never heard of it before.

          I know some Ruby/Rails devs who use Guzzle a lot, but I've never used it myself.

            I am going to leave this out here for anyone else using PHP trying to interact w/ UPS's Electronic Manifesting's Package Level Details using TLS1.2. Or simply UPS PLD using PHP cURL.

            Might save others some time.

            NogDog... thanks for the replies.

            //set basic creds
            $user = "user";
            $pass = "pass";
            $targetDIR = '/hapld/tos/kdwhapltos';
            $port = '443';
            $url = 'https://www.pld-certify.ups.com'.$targetDIR.'';
            
            //get your manifest data to send
            $body = '020094                        2011R0    2014093000000012939600400909000000001*AA111111    US0               0129396004000001*BA1Z1111110300002095                 00001+0000000000000070 +0000000000000000LBS03PRE10                                   3INUSD000001*CA18TEST ORDER                         TEST ORDER                         10800 E 45TH AVE                                                                                         DENVER                        CO   80239    US15555555555                                             *PA1Z1111110300002095                 02+0000070                                                                                      +0000000+00000000+00000000+00000000*SA000004';
            
            
            //Build PLD Request
            $PLDRequest = 
            "--BOUNDARY\r\n
            Content-type: application/x-www-form-urlencoded\r\n
            Content-length: 128\r\n \r\n
            AppVersion=1.0&AcceptUPSLicenseAgreement=YES&ResponseType=application/x-ups-pld&VersionNumber=V4R1&UserId=".$user."&Password=".$pass."\r\n r\n
            --BOUNDARY\r\n
            Content-type: application/x-ups-binary\r\n
            Content-length: ".strlen($body)."\r\n \r\n
            ".$body."\r\n\r\n
            --BOUNDARY--"; 
            
            
            // using curl for upload 
            $ch = curl_init(); 
            curl_setopt($ch, CURLOPT_URL, $url); 
            curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/mixed; boundary=BOUNDARY", "Content-length: ".strlen($PLDRequest)."")); 
            curl_setopt($ch, CURLOPT_POST, 1); 
            curl_setopt($ch, CURLOPT_POSTFIELDS, $PLDRequest); 
            curl_setopt($ch, CURLOPT_HEADER, 0); 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
            curl_setopt($ch, CURLOPT_SSLVERSION, 6); //6 = TLS1.2
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_CAINFO, "/www/path/to/your/cacert.pem");
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST');
            
            $result = curl_exec($ch);
            $errNo = curl_errno($ch);
            $err = curl_error($ch);
            curl_close($ch); 
            
            /*
            //basic error reporting if needed
            echo '<br/><hr/><br/>';
            echo 'error no: ' . $errNo;
            echo '<br/>';
            echo 'errMsg: ' . $err;
            echo '<br/>';
            */
            echo 'Response: ';
            var_dump($result);
            
              Write a Reply...