Solved

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

Posted on 2008-10-20
4
757 Views
Last Modified: 2013-11-10
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

0
Comment
Question by:LyleHolmes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:MatthiasVance
ID: 22765229
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

0
 
LVL 8

Accepted Solution

by:
MatthiasVance earned 125 total points
ID: 22765238
Sorry, I forgot to add an example.
<?php
echo postHttps("https://webservice/", utf8_encode(file_get_contents("data.xml")));
?>

Open in new window

0
 

Author Comment

by:LyleHolmes
ID: 22767208
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?
0
 

Author Comment

by:LyleHolmes
ID: 22772390
Hey Matthias,
Your code worked very well.
Thanks for the input.
Lyle
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For some reason Google Apps has decided to reserve 'www' for your new domain which results in an error when trying to map the web address 'www' to a certain site in your Google Sites. The error is 'this address is already being used' and after some …
These instructions are based on installing Owncloud on your new raspberry pi connected with a usb HDD. What do you need Part A? A Raspberry Pi, model B. A boot SD card for the Raspberry Pi. A usb HDD An Ethernet cable to connect to the lo…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question