Wednesday, May 6, 2009

JAX-WS Part 2: PHP-CURL sample client

I'll keep the PHP code minimalistic and will not be creating a SOAP client with PHP. Instead I'll just post a ready-made SOAP xml using CURL and my client certificat/key pair. So the PHP code looks like this:


<?php

// connect to server with client cert and private key
if ($ch = curl_init()){

curl_setopt($ch, CURLOPT_URL, "https://localhost:8181/ExampleWS/Example");
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, 'CURLOPT_SSLCERTTYPE', 'PEM');
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLCERT, 'democompany1.crt');
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'changeit');
curl_setopt($ch, CURLOPT_SSLKEY, 'democompany1.key');
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, 'changeit');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CAINFO, NULL);
curl_setopt($ch, CURLOPT_CAPATH, NULL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml', 'SOAPAction: ""') );
curl_setopt($ch, CURLOPT_POST, 1);
$soap_message = <<<SOAP
<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Body>
<ns2:sampleEcho xmlns:ns2=""><sampleParamStr>Hello World!</sampleParamStr></ns2: sampleEcho >
</env:Body>
</env:Envelope>
SOAP;
curl_setopt($ch, CURLOPT_POSTFIELDS, $soap_message);
echo "Connecting\n";
$data = curl_exec($ch);
if ($data) {
echo "DATA: ".$data;
} else {
echo curl_error($ch);
}
echo "\nDone\n";

}


?>


You can run this with PHP-CLI

php curlsample.php

No comments:

Post a Comment