在PHP中创建SOAP和XML请求并显示响应

时间:2015-03-27 18:32:28

标签: php xml web-services curl soap

我需要向一个CRM软件发送一个肥皂请求我试图在PHP中集成,我知道我应该使用cURL并且已经按照这个答案here来设置请求,但没有运气让它工作。

下面我列出了我需要发送的请求示例以及我希望能够在我的页面上输出的响应示例。

SOAP请求

POST /xmlreceive.asmx HTTP/1.1
Host: dummyurl.co.uk
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <CaseApplicationZipped xmlns="http://tempuri.org">
      <XMLApplication>XML HERE</XMLApplication>
      <byArray>base64Binary</byArray>
    </CaseApplicationZipped>
  </soap12:Body>
</soap12:Envelope>

SOAP响应

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <CaseApplicationZippedResponse xmlns="http://tempuri.org">
      <CaseApplicationZippedResult>string</CaseApplicationZippedResult>
    </CaseApplicationZippedResponse>
  </soap12:Body>
</soap12:Envelope>

我的XML

$XML = '';
$XML .= '<Application><Cases><Case>';
$XML .= '<CreateJob>0</CreateJob>';

$XML .= '<Title>Mr</Title>'; // Title
$XML .= '<FirstName>Test</FirstName>'; // First Name
$XML .= '<LastName>Testing</LastName>'; // Last Name

$XML .= '<HouseNumber>123</HouseNumber>'; // House Number
$XML .= '<HouseName></HouseName>'; // House Name
$XML .= '<Address1>Test Street</Address1>'; // Address 1
$XML .= '<Address2></Address2>'; // Address 2
$XML .= '<Address3>Test</Address3>'; // Address 3
$XML .= '<Address4>Test</Address4>'; // County
$XML .= '<PostCode>T35 7NG</PostCode>'; // Postcode
$XML .= '<HomeTelephone>01234 567 890</HomeTelephone>'; // Telephone
$XML .= '<MobileTelephone>09876 543 210</MobileTelephone>'; // Mobile
$XML .= '<Email>test@test.com</Email>'; // Email

$XML .= '<Referer></Referer>'; // Referrer

$XML .= '<Notes>Test Notes</Notes>'; // Notes

$XML .= '<SourceName>'. 'Portal - Test' .'</SourceName>'; // Source
$XML .= '<SourceAffName>Test Source Aff</SourceAffName>'; // Source Affiliate

$XML .= '<CustomerType></CustomerType>'; // Customer Type

$XML .= '</Case></Cases></Application>';

我尝试过的事情

$soapUrl = "http://www.dummyurl.co.uk/XMLReceive.asmx";

$xml_post_string = '';
$xml_post_string .= '<?xml version="1.0" encoding="utf-8"?>';
$xml_post_string .= '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">';
$xml_post_string .= '<soap12:Body>';
$xml_post_string .= '<CaseApplicationZipped xmlns="http://tempuri.org">';
$xml_post_string .= '<XMLApplication>' . $XML . '</XMLApplication>';
// $xml_post_string .= '<byArray>' . '</byArray>';
$xml_post_string .= '</CaseApplicationZipped>';
$xml_post_string .= '</soap12:Body>';
$xml_post_string .= '</soap12:Envelope>';

$headers = array(
    "POST /xmlreceive.asmx HTTP/1.1",
    "Host: www.dummyurl.co.uk",
    "Content-Type: application/soap+xml; charset=utf-8",
    "Content-Length: ".strlen($xml_post_string)
); 

$url = $soapUrl;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch); 
curl_close($ch);

$response1 = str_replace("<soap12:Body>","",$response);
$response2 = str_replace("</soap12:Body>","",$response1);

$parser = simplexml_load_string($response2);

有人能指出我正确的方向来完成这项工作并捕获并显示响应吗?

由于

0 个答案:

没有答案