Link to home
Start Free TrialLog in
Avatar of LyleHolmes
LyleHolmes

asked on

How do I send HTTPS POST to web service using XML and PHP?

We have to send a request to a web service using HTTPS POST.  The code below is the XML that we will send. It sends a UPC number that comes from our MySQL database on an Apache server. The web service then responds with the product details associated with the UPC number. We send the POST to:

https://Secure.Website.Com/Webservices/upc.Exe/onlinestore

We are planning to use PHP to create and process the POST. We're trying to keep it simple.

Is this correct? We create a simple html form that pulls the UPC from our database (no problem there).  Our html form uses a php script that we create for the action in the form. This PHP script assembles the XML request and passes it via HTTPS POST to the web service.

Assuming this is essentially correct, we are looking for any samples of the PHP script necessary to assemble the data and process the POST.

Thanks a bunch.
<TRANSACTION>
 
   <RNCERT>BDF95</RNCERT>
 
   <TRANSACTIONTYPE>GETUPCINVENTORY</TRANSACTIONTYPE>
 
   <UPC>883929015368</UPC>
 
   <SHOWITEMDETAILS>TRUE</SHOWITEMDETAILS>
 
 </TRANSACTION>

Open in new window

Avatar of MatthiasVance
MatthiasVance
Flag of Netherlands image

Assuming you have curl installed, you can do the following.

Kind regards,

Matthias Vance
<?php
 
function postHttps($url, $request) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POST, 1) ;
	curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	$result = curl_exec($ch);
	curl_close($ch);
	return $result;
}
 
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of MatthiasVance
MatthiasVance
Flag of Netherlands 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 LyleHolmes
LyleHolmes

ASKER

Thanks Matthias,

I assume the php/echo is on the page with "https://webservice" being replaced by the actual URL that we are going to post to.

Do we define the fields being delivered within the CURL using variables?
Hey Matthias,
Your code worked very well.
Thanks for the input.
Lyle