PHP& XML - 如何生成soap请求错误415

时间:2014-11-02 11:56:54

标签: php xml curl soap

如果有人可以帮助我,为什么我会收到错误415 我不明白为什么会发生错误 链接到错误 http://agency.lastminute-hr.com/stranice/upisi_destinacije_unico.php

这是xml的方式

POST /services/WebService.asmx HTTP/1.1
Host: wl.filos.com.gr
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>
        <PlaceSearch xmlns="http://www.cyberlogic.gr/webservices/">
          <xml>string</xml>
        </PlaceSearch>
      </soap12:Body>
    </soap12:Envelope>

我的代码是:

$soapUrl = "http://wl.filos.com.gr/services/WebService.asmx?op=PlaceSearch";



$soap_request  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  $soap_request .= "<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/\">\n";
  $soap_request .= " <soap:Body>\n";
  $soap_request .= "  <PlaceSearch xmlns=\"http://www.cyberlogic.gr/webservices/\">\n";
  $soap_request .= "  <xml><PlaceSearchRequest><Username>******</Username><Password>******</Password><PlaceType>Cities</PlaceType><Language>en</Language></PlaceSearchRequest></xml>\n";
  $soap_request .= "   </PlaceSearch>\n";
  $soap_request .= "  </soap:Body>\n";
  $soap_request .= "</soap:Envelope>";
 $xml_post_string  =  $soap_request;



$headers = array(
"POST /services/WebService.asmx HTTP/1.1",
"Host: wl.filos.com.gr",
"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); 

$info = curl_getinfo($ch);

     // echo results
        echo "The server responded: <br />";
        echo   " " .  $info['http_code'].  "  <br />";

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

$parser = simplexml_load_string($response2);

2 个答案:

答案 0 :(得分:1)

415是您从使用curl触发的HTTP请求的响应中获得的 HTTP状态代码

这些代码为standardized and documented,在您的情况下为415:

  

415不支持的媒体类型

     

服务器拒绝为请求提供服务,因为请求的实体采用所请求方法所请求资源不支持的格式。

请求的实体表示请求主体,它是POST方法的请求主体。简而言之,这意味着您发送到服务器的数据不符合它的需要。

您必须先修复发送到服务器的数据,否则所有后续操作(如将响应字符串加载到simplexml中)也会失败。

或者如果你确定你正确地遵循了webservice的规范,你唯一能做的就是正确的错误处理,也就是说,如果服务器为你的请求返回一个错误(代码400到499)而不处理进一步返回值,但只是发出错误信号。

答案 1 :(得分:0)

此XML对您使用的Web服务无效。

将其粘贴到此处:http://wl.filos.com.gr/services/WebService.asmx?op=PlaceSearch并检查出现了什么问题