Link to home
Start Free TrialLog in
Avatar of Marthaj
MarthajFlag for United States of America

asked on

PHP, Curl and XML

I am having a problem with some coding with PHP 5.6 and curl sending/receiving XML.
The error that I am receiving is: "Server was unable to process request. ---> Object reference not set to an instance of an object."
which I know can be caused by several things : missing/incorrect xml; incorrect urls; datatype not matching the WSDL document etc.
I have attached the example of the XML that was sent to be to use as a guideline. I have checked the WSDL document and everything in my coding looks fine. The data sent looks fine too.
 Additionally, using Fiddler 4 - it shows the correct username,password and booking nbr (Note, I starred out the actual data sent but those items are correct.)
but also shows this: txtbook=*****&txtuser=*****&txtpass=*****&retreive=Retreive+Booking

I am very curious about this part of it: retreive=Retreive+Booking

I have asked the vendor to verify all links and the correctness of the WSDL document.
But in the meanwhile, I wanted to check my coding too.
Could someone be as so kind as to review my coding? It would be appreciated...I can provide the actual URL name instead of "somesite" but only privately...sigh
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Import</title>
<br><br>
<link rel="stylesheet" href="jquerycustom/jquery-ui.min.css">
<script type="text/javascript" src="jquerycustom/external/jquery/jquery.js"></script>
<script type="text/javascript" src="jquerycustom/jquery-ui.min.js"></script>
</HEAD>

 <?php 
// next line for development
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
//next line for production
// error_reporting(0);   
   // header ("cache-Control: no-cache");
   $baderr= "";

    if (isset($_POST['exit']))
        {
		    // REMOVE SESSION DATA
            $_SESSION = array();

		    // REMOVE SESSION COOKIE
            setcookie(session_name(), '', time()-3600, '/');
    
            // DESTROY SESSION
            session_destroy();
           // need to add exit location

		   exit;
        }

    if (isset($_POST['retreive']))
        {
			$errmsg = '';
			$txtbookingid  = '';
			$txtusername = '';
			$txtpassword = '';
		
			// pull data 
			$txtbookingid = intval($_POST['txtbook']);
			$txtusername = $_POST['txtuser'];
			$txtpassword = $_POST['txtpass'];
			echo $txtbookingid .'<br>';
			echo $txtusername .'<br>';
			echo $txtpassword .'<br>';
			
			// begin data validation
			
        	//check to see if the bookingid username or password are empty 
			if (strlen($txtbookingid) ==0 or !intval($txtbookingid))
			    {
					if (strlen($txtbookingid) ==0)
					{
				      $errmsg = $errmsg. 'Booking Id is Required<br>';
					}
					if (!intval($txtbookingid))
					{
				      $errmsg = $errmsg. 'Booking Id MUST be Numeric<br>';
					}
				}
				
			if (strlen($txtusername) ==0) 
			    {
				    $errmsg = $errmsg . 'User Name is Required<br>';
				}
			if (strlen($txtpassword) ==0) 
			    {
				    $errmsg = $errmsg . 'Password is Required<br>';
				}
		    if (strlen($errmsg) != 0) 
			    {
			       echo '<div id=dialog title=ERRORS:><p>' . $errmsg . '</p></div>';
			    }else{
					
	
/*					
	
$xml = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
               xmlns:xsd="http://www.w3.org/2001/XMLSchema "
               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
      <GatewaySoapAuthentication xmlns="http://ws.somesite.com/">
        <Username>$txtusername</Username>
        <Password>$txtpassword</Password>
      </GatewaySoapAuthentication>
    </soap:Header>
  <soap:Body>
    <GetBookingDetails xmlns="http://ws.somesite.com/">
      <BookingDetailsRequest>
        <BookingNumber>$txtbookingid</BookingNumber>
      </BookingDetailsRequest>
    </GetBookingDetails>
  </soap:Body>
</soap:Envelope>
EOD;
*/

$xml ='<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <GatewaySoapAuthentication xmlns="http://ws.somesite.com/">
      <Username>' . $txtusername . '</Username>
      <Password>' . $txtpassword . '</Password>
    </GatewaySoapAuthentication>
  </soap:Header>
  <soap:Body>
    <GetBookingDetails xmlns="http://ws.somesite.com/">
      <BookingDetailsRequest>
        <BookingNumber>' . $txtbookingid . '</BookingNumber>
      </BookingDetailsRequest>
    </GetBookingDetails>
  </soap:Body>
</soap:Envelope>';

// CREATE THE HEADER 
$header =
[ 'Host: ws.somesite.com'
, 'Content-type: text/xml; charset=utf-8'
, 'Content-length: ' . strlen($xml)
, 'SOAPAction: http://ws.somesite.com/GetBookingDetails'
, 'Connection: close'
]
;



//CREATE THE URL
// original url link from documentation...documentation dated 2008
$url = 'https://ws.somesite.com/test/FlightGateway.asmx';

$checkstring = 'AuthenticationHeaderException : The Username or Password is invalid';

$ca = 'c:\wamp\ca-bundle\cacert.pem';	

  	    		// setup curl parameters
					$ch = curl_init();
					curl_setopt($ch,CURLOPT_URL,$url);
					curl_setopt($ch,CURLOPT_CAINFO,$ca);
                                       curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
					curl_setopt($ch,CURLOPT_POST,TRUE);
					curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
					curl_setopt($ch,CURLOPT_TIMEOUT,300);
					curl_setopt($ch,CURLOPT_POSTFIELDS, $xml);
					$data = curl_exec($ch);
					
					//var_dump($data);
					//exit;
					//var_dump((curl_errno($ch)));
					//echo '<br>';
					$nbr = 0;
					$nbr = strpos($data,$checkstring);
					if ($nbr > 0)
					{
			            echo '<div id=dialog title=ERRORS:><p>The Username or Password is invalid</p></div>';
					}
					
					if(curl_errno($ch))
					{
						echo 'at error rtn' . '<br>';
						$errmsg = curl_error($ch);
						var_dump($errmsg);
			            echo '<div id=dialog title=ERRORS:><p>' . $errmsg . '</p></div>';
						print curl_error($ch);
					    curl_close($ch);
					}else{
						echo 'AT GOOD RTN<br>';
						curl_close($ch);
					    //echo '<div id=dialog title=Data Returned:><p>' . $data . '</p></div>';
                        if($data)
						{
					      echo '<div id=dialog title=Data><p>' . $data . '</p></div>';
						}else{
						  echo '<div id=dialog title=No Match Found for:><p></p>' . $txtbookingid . '</div>';
					    }
						
					}
					
				}
        }
	
 ?>

 <BODY>
 <script> 
  
  $(function() {
    $( "#dialog" ).dialog();
  } );
  
  </script>
 <H1 align=center>Import</H1>
<P>&nbsp;</P>
	<br><br>
    <div>
<!--- start form -->
        <form action="TestGetBooking.php" method="post">
            <table style="margin: auto;">
                <tr>
                    <td style="text-align:center">Retreive a Booking</td>
                </tr>
            </table>
            <br><br>
            <table style="margin:auto;">
				<tr>
				    <td style="text-align:right">Enter Booking ID: 
                        <input type="number" name="txtbook" id="txtbook" title="Enter MyCompany Booking ID">
					</td>
				</tr>
				<tr>
				    <td></td>
				</tr>
				<tr>
  				    <td style="text-align:right">UserName:
					<input type="text" name="txtuser" id="txtuser" title="Enter your MyCompany user name">
					</td>
				</tr>
				<tr>
				     <td></td>
				</tr>
				<tr>
					<td style="text-align:right">Password:
				        <input type="password" name="txtpass" id="txtpass" title="Enter your MyCompany password">
                    </td>
                </tr>				
			</table>
			<br><br>
	        <table style="margin: auto;">
                <tr>
                    <td><input type="submit" name="retreive" value="Retreive Booking" src="" alt="Retreive Booking" />
                    <td><input type="submit" name="cancel" value="Cancel" src="" alt="Cancel"></td>
                    <td><input type="reset" name="reset" value="Reset Form" src="" alt="Reset Form"></td>
			    </tr>
            </table>
        </form>
<!-- end form -->			
     </div>
</body>
</html> 

Open in new window

POST.doc
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Have you tried "retrieve" instead of "retreive"  for the submit button name, and also "Retrieve" in the button value?
Avatar of Marthaj

ASKER

I will try it now...
Avatar of Marthaj

ASKER

I changed it, tested it and same results...I also for laughs and giggles - changed to reflect the method in documentation: GetBookingDetails.
Is my coding correct?
We need the credentials (line 9-11) in order to test.  Also, your code snippet mentions "MyCompany" in the HTML input controls.  Is that what should replace "somesite?"
<?php
/**
 * https://www.experts-exchange.com/questions/29025211/PHP-Curl-and-XML.html
 */
error_reporting(E_ALL);
echo '<pre>';

// SET CREDENTIALS FOR THE XML DOCUMENT
$txtusername = '??';
$txtpassword = '??';
$bookingid   = '??';

$xml = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
               xmlns:xsd="http://www.w3.org/2001/XMLSchema "
               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
      <GatewaySoapAuthentication xmlns="http://ws.somesite.com/">
        <Username>$txtusername</Username>
        <Password>$txtpassword</Password>
      </GatewaySoapAuthentication>
    </soap:Header>
  <soap:Body>
    <GetBookingDetails xmlns="http://ws.somesite.com/">
      <BookingDetailsRequest>
        <BookingNumber>$bookingid</BookingNumber>
      </BookingDetailsRequest>
    </GetBookingDetails>
  </soap:Body>
</soap:Envelope>
EOD;


// CREATE THE HEADER
$header =
[ 'Content-type: text/xml; charset=utf-8'
, 'Content-length: ' . strlen($xml)
, 'SOAPAction: http://ws.somesite.com/GetBookingDetails'
, 'Connection: close'
]
;

//CREATE THE URL
$url = trim('http://ws.somesite.com/FlightGateway.asmx');
echo PHP_EOL . "URL: $url";

// SET UP THE cURL REQUEST
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL,            $url           );
curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header        );
curl_setopt( $curl, CURLOPT_POST,           TRUE           );
curl_setopt( $curl, CURLOPT_POSTFIELDS,     $xml           );
curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate' );
curl_setopt( $curl, CURLOPT_TIMEOUT,        5              );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE           );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE           );
curl_setopt( $curl, CURLOPT_FAILONERROR,    TRUE           );

// IF USING SSL, THIS INFORMATION IS IMPORTANT -- UNDERSTAND THE SECURITY RISK!
curl_setopt( $curl, CURLOPT_SSLVERSION,     CURL_SSLVERSION_DEFAULT  );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2  ); // DEFAULT
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, 1  ); // DEFAULT

// SET THE LOCATION OF THE COOKIE JAR (THIS FILE WILL BE OVERWRITTEN)
curl_setopt( $curl, CURLOPT_COOKIEFILE,     'cookie.txt' );
curl_setopt( $curl, CURLOPT_COOKIEJAR,      'cookie.txt' );

// RUN THE CURL REQUEST AND GET THE RESULTS
$document  = curl_exec($curl);
$errno     = curl_errno($curl);
$info      = curl_getinfo($curl);
$http_code = $info['http_code'];
$errmsg    = curl_error($curl);
curl_close($curl);

// SHOW THE WORK PRODUCT
echo PHP_EOL . 'HTTP CODE: ' . htmlentities($http_code);
echo PHP_EOL . 'cURL ERRNO: ' . htmlentities($errno);
echo PHP_EOL . 'cURL ERROR: ' . htmlentities($errmsg);
echo PHP_EOL; print_r($info);
echo PHP_EOL . 'DOCUMENT: ' . htmlentities($document);

// SHOW THE COOKIES, IF ANY
echo PHP_EOL . PHP_EOL;
echo @file_get_contents('cookie.txt');

Open in new window

Avatar of Marthaj

ASKER

Hello Ray! Yes MyCompany [obfuscated - Modalot]. thank you for helping. I know the username/password/booking nbr are valid.
I think I still need the credentials for username, password, and booking ID.  I'm not getting a sensible response from their API.  I've tried several things, including an empty request.  With the empty request, I get a 400 Bad Request response, which makes sense.  But as soon as I add any of the SOAP to the XML, it falls over to 500 Internal Server Error.

It's always something like this...
URL: http://ws.mycompany.com/FlightGateway.asmx
HTTP CODE: 500
cURL ERRNO: 22
cURL ERROR: The requested URL returned error: 500 Internal Server Error
Array
(
    [url] => http://ws.mycompany.com/FlightGateway.asmx
    [content_type] => 
    [http_code] => 500
    [header_size] => 25
    [request_size] => 261
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.192042
    [namelookup_time] => 0.053744
    [connect_time] => 0.081707
    [pretransfer_time] => 0.081827
    [size_upload] => 33499
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 174435
    [download_content_length] => -1
    [upload_content_length] => 33499
    [starttransfer_time] => 0.107776
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 70.42.217.162
    [certinfo] => Array
        (
        )

    [primary_port] => 80
    [local_ip] => 69.65.27.131
    [local_port] => 48968
)

DOCUMENT: 

Open in new window

BTW, the same result occurs when using their test URL
https://ws.mycompany.com/test/FlightGateway.asmx
Avatar of Marthaj

ASKER

okay, I will send it to you at gmail acct.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marthaj

ASKER

Nothing to be sorry about - just know I appreciated you taking the time to help out. Sent you email.
Avatar of Marthaj

ASKER

Thank you!
Here's the script I used.
<?php // demo/mycompany.php
/**
 * https://www.experts-exchange.com/questions/29025211/PHP-Curl-and-XML.html
 *
 * https://curl.haxx.se/libcurl/c/libcurl-errors.html
 *
 * https://ws.mycompany.com/test/FlightGateway.asmx?op=GetBookingDetails
 */
error_reporting(E_ALL);
echo '<pre>';


// SET CREDENTIALS FOR THE XML DOCUMENT
$txtusername = '??';
$txtpassword = '??';
$txtbookingid = '??';


$xml = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <GatewaySoapAuthentication xmlns="http://ws.somesite.com/">
      <Username>$txtusername</Username>
      <Password>$txtpassword</Password>
    </GatewaySoapAuthentication>
  </soap:Header>
  <soap:Body>
    <GetBookingDetails xmlns="http://ws.somesite.com/">
      <BookingDetailsRequest>
        <BookingNumber>$txtbookingid</BookingNumber>
      </BookingDetailsRequest>
    </GetBookingDetails>
  </soap:Body>
</soap:Envelope>
EOD;


// CREATE THE HEADER
$header =
[ 'Content-type: text/xml; charset=utf-8'
, 'Content-length: ' . strlen($xml)
, 'SOAPAction: http://ws.mycompany.com/GetBookingDetails'
, 'Connection: close'
]
;

// CREATE THE URL
$url = 'http://ws.mycompany.com/test/FlightGateway.asmx';
echo PHP_EOL . "URL: $url";

// SET UP THE cURL REQUEST
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL,            $url           );
curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header        );
curl_setopt( $curl, CURLOPT_POST,           TRUE           );
curl_setopt( $curl, CURLOPT_POSTFIELDS,     $xml           );
curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate' );
curl_setopt( $curl, CURLOPT_TIMEOUT,        5              );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE           );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE           );
curl_setopt( $curl, CURLOPT_FAILONERROR,    TRUE           );

// IF USING SSL, THIS INFORMATION IS IMPORTANT -- UNDERSTAND THE SECURITY RISK!
// curl_setopt( $curl, CURLOPT_SSLVERSION,     CURL_SSLVERSION_DEFAULT  );
// curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2  ); // DEFAULT
// curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, 1  ); // DEFAULT

// SPECIFIED BY THE AUTHOR
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

// SET THE LOCATION OF THE COOKIE JAR (THIS FILE WILL BE OVERWRITTEN)
curl_setopt( $curl, CURLOPT_COOKIEFILE,     'cookie.txt' );
curl_setopt( $curl, CURLOPT_COOKIEJAR,      'cookie.txt' );

// RUN THE CURL REQUEST AND GET THE RESULTS
$document  = curl_exec($curl);
$errno     = curl_errno($curl);
$info      = curl_getinfo($curl);
$http_code = $info['http_code'];
$errmsg    = curl_error($curl);
curl_close($curl);

// SHOW THE WORK PRODUCT
echo PHP_EOL . 'HTTP CODE: ' . htmlentities($http_code);
echo PHP_EOL . 'cURL ERRNO: ' . htmlentities($errno);
echo PHP_EOL . 'cURL ERROR: ' . htmlentities($errmsg);
echo PHP_EOL; print_r($info);
echo PHP_EOL . 'DOCUMENT: ' . htmlentities($document);

// SHOW THE COOKIES, IF ANY
echo PHP_EOL . PHP_EOL;
echo @file_get_contents('cookie.txt');

Open in new window

Avatar of Marthaj

ASKER

Thank you!