Sending HTTPS POST Message with XML Content



Currently, there are many sites that make XML available and we need to send HTTPS POST message request properly in order to get the result back.

To do that by using PHP on Windows environment, we need to ensure these following requirements.

1)  We should check the XML content whether it is well-formed and valid before we send the message.  This can be done by using various tools, such as Eclipse with XML plugin and NetBeans 5.0

2)  For the URL that is HTTP over SSL, we need to use CURL library.  That is we need to enable CURL support which can be done as follows

2.1) Go to file php.ini,  uncomment these two lines to use CURL and OPENSSL

2.2) Make sure that libeay32.dll and ssleay32.dll must be present in the PATH environment variable.

If we successfully enable CURL,  when we check with phpinfo() funciton, we will get

3)  Use CURL functions in the PHP program that we connect to GHRS


$data = ""  xmlns:xsi="" xsi:schemaLocation="" Target="Test" Version="1.000" SequenceNmbr="1">Test Klub";

// Init curl
$ch = curl_init(); 

// Set Curl Option: Set url for the post
curl_setopt($ch, CURLOPT_URL,$URL);

// Set Curl Option: Enable http post
curl_setopt($ch, CURLOPT_POST, 1);

// Set Curl Option: Set post data
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

// Set Curl Option: Enable http header setting
curl_setopt($ch, CURLOPT_HEADER, true);

// Set Curl Option: Specify that http content is XML through HTTP header
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml'));

// Set Curl Option: Collect result from script
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Set Curl Option: Set timeout to 15 seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 15);

// tell cURL to graciously accept an SSL certificate if presented
 if(ereg("^(https)",$URL)) curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);

// Execute and send HTTP POST message
$result = curl_exec ($ch);   

// Check whether there is error
if (curl_errno($ch)) {
   print curl_error($ch);
} else {

echo $result;

If we successfully send the request, we will get the response as shown below:




บันทึกนี้เขียนที่ GotoKnow โดย  ใน เรียนรู้เทคโนโลยีคอมพิวเตอร์

หมายเลขบันทึก: 28166, เขียน: , แก้ไข, 2013-09-06 17:22:31+07:00 +07 Asia/Bangkok, สัญญาอนุญาต: สงวนสิทธิ์ทุกประการ, อ่าน: คลิก

คำสำคัญ (Tags) #uncategorized


ความเห็น (0)