Solved

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

Posted on 2008-10-20
4
754 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
  • 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

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…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

813 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now