Jump to content

Consuming Web Services


ch0311

Recommended Posts

A friend of mine is a web designer for a casino in las vegas. She wants me to get this one section working using php to consume a web service.

I created the form with the correct name fields and sent it over. That worked fine; but when I click the submit button the web service sends me back an xml file. So I thought I needed to use curl to send the info; then use the xml response as I would a normal array.

Here is the web service I am trying to consume.
[code]
POST /optin.asmx HTTP/1.1
Host: www.stationcasinosemail.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.stationcasinosemail.local/optin/PostEmail"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <PostEmail xmlns="http://www.stationcasinosemail.local/optin">
      <strDOBDay>string</strDOBDay>
      <strDOBMonth>string</strDOBMonth>
      <strDOBYear>string</strDOBYear>
      <strEmail>string</strEmail>
      <strFName>string</strFName>
      <strLName>string</strLName>
      <strSource>string</strSource>
      <strRing>string</strRing>
    </PostEmail>
  </soap:Body>
</soap:Envelope>
[/code]

[code]
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <PostEmailResponse xmlns="http://www.stationcasinosemail.local/optin">
      <PostEmailResult>
        <ResponseCode>int</ResponseCode>
        <ResponseMessage>string</ResponseMessage>
      </PostEmailResult>
    </PostEmailResponse>
  </soap:Body>
</soap:Envelope>
[/code]

Here is the code I am using.

[code]
error_reporting( E_ALL);

$xml = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <PostEmail xmlns="http://www.stationcasinosemail.local/optin">
      <strDOBDay>25</strDOBDay>
      <strDOBMonth>04</strDOBMonth>
      <strDOBYear>1980</strDOBYear>
      <strEmail>[email protected]</strEmail>
      <strFName>Test</strFName>
      <strLName>Testing</strLName>
      <strSource>corporate</strSource>
      <strRing>stationcasinos</strRing>
    </PostEmail>
  </soap:Body>
</soap:Envelope>
EOD;

$header[] = "Content-type: text/xml; charset=utf-8";
$header[] = "Content-length: " . strlen( $xml);
$header[] = 'SOAPAction: "http://www.stationcasinosemail.local/optin/PostEmail"';

$url = "http://www.stationcasinosemail.com/optin.asmx/PostEmail";

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt( $ch, CURLOPT_HEADER, 1);
curl_setopt( $ch, CURLOPT_TIMEOUT, 4);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $header);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $xml);

$result = curl_exec( $ch);

echo '<pre>';
print_r( $result);
echo '</pre>';
[/code]

Here is the error that I am getting back from the soap server.

[code]
HTTP/1.1 500 Internal Server Error.
Date: Wed, 01 Nov 2006 04:04:49 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Content-Length: 300

System.InvalidOperationException: Request format is invalid: text/xml; charset=utf-8.
  at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
  at System.Web.Services.Protocols.WebServiceHandler.Invoke()
  at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
[/code]

I have tried to get this working for 2 nights. If anybody could let me know what I am doing wrong; please let me know.

Thanks,

Chris
Link to comment
https://forums.phpfreaks.com/topic/25770-consuming-web-services/
Share on other sites

I got it done. Below is the code that got it done.

[code]
error_reporting( E_ALL);

$url = "http://www.stationcasinosemail.com/optin.asmx/PostEmail";
$params = 'strDOBDay=25&strDOBMonth=04&strDOBYear=1980&[email protected]&strFName=John&strLName=Doe&strSource=Corporate&strRing=StationCasinos';

$ch = curl_init();

curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt ($ch, CURLOPT_HEADER, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec( $ch);

echo '<pre>';
print_r( $result);
echo '</pre>';
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.