【问题标题】:Sending xml to webservices using cURL使用 cURL 将 xml 发送到 Web 服务
【发布时间】:2015-06-16 10:45:03
【问题描述】:

为了让您大致了解我在这里要完成的工作,我正在运行一个带有自定义主题的 WordPress 网站,并利用重力表单插件在网站上创建所有表单。

我们正在将网站上的所有 (2) 个表单与外部潜在客户管理服务集成。当然,他们关于如何与他们的 web 服务交互的文档远没有得到很好的记录。

提供的 PDF 说明:

假设第三方供应商熟悉创建 XML 文件,了解 XSD 文档以创建格式良好的 XML 文档,并具有通过 Web 服务提交和接收 XML 文档所需的适当工具.

网络服务网址:

https://interface.webservices.popcard.ltsolutions.com/service.asmx

网络服务方法

插入流量

InsertTraffic 是第三方供应商用于将单个流量插入 Yardi PopCard 的 PopCard 应用程序的方法。这一单条流量可以代表潜在客户在 ILS 网站上填写联系表格,或从潜在客户那里接听并由呼叫中心接听的电话。 - PDF 中的方法描述

我已与该公司联系,并知道我尝试推送到他们的 Web 服务的 XML 是正确的,但线索没有进入系统。

<?php 
add_action("gform_after_submission", "submit_contact_lead", 10, 2);

function submit_contact_lead($entry, $form){

  $fname = $entry['1.3'];
  $lname = $entry['1.6'];
  $userEmail = $entry['2'];
  if ($entry['3']) {
    $comments = $entry['3'];
  } else {
    $comments = '';
  }
  $date = date('Y-m-d\TH:i');
  $baseURL = 'http://interface.webservices.popcard.ltsolutions.com/service.asmx/InsertTraffic';
  $xmlRequest = '<?xml version="1.0" encoding="utf-8"?>
    <traffic 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        contactdatetime="'.$date.'" 
        transactiondatetime="'.$date.'">
    <trafficsource>
        <vendorid>551d12d8a1de</vendorid>
        <emailaddress></emailaddress>
        <sourcename></sourcename>
        <propertyname>CityView</propertyname>
    </trafficsource>
    <prospect>
        <firstname>'.$fname.'</firstname>
        <middlename></middlename>
        <lastname>'.$lname.'</lastname>
        <streetaddress1></streetaddress1>
        <streetaddress2></streetaddress2>
        <city></city>
        <state></state>
        <zipcode></zipcode>
        <daytimephone></daytimephone>
        <eveningphone></eveningphone>
        <cellphone></cellphone>
        <otherphone></otherphone>
        <emailaddress>'.$userEmail.'</emailaddress>
        <comments>'.$comments.'</comments>
      </prospect>
    <prospectpreferences>
        <pricerangemin></pricerangemin>
        <pricerangemax></pricerangemax>
        <numberofoccupants></numberofoccupants>
        <pets></pets>
        <dateneeded></dateneeded>
        <appointmentdate></appointmentdate>
        <appointmenttime></appointmenttime>
        <numberofbedsdesired></numberofbedsdesired>
        <numberofbathsdesired></numberofbathsdesired>
    </prospectpreferences>
    </traffic>';

    $xmlRequest = preg_replace( "/\r|\n/", "", $xmlRequest );

    // Set up cURL request directly in this funtion
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://interface.webservices.popcard.ltsolutions.com/service.asmx');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "InsertTraffic=" . $xmlRequest);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); // set to 300 after testing purposes
    curl_setopt($ch, CURLOPT_TIMEOUT, 0); // set to 300 after testing purposes
    $result = curl_exec($ch);
    curl_close($ch);

    // $array_data = json_decode(json_encode(simplexml_load_string($data)), true);

    error_log('PopCards Submission | Contact/Reserve Submission for '.$lname.', '. $fname.'.');
    error_log($xmlRequest);
    error_log($result);
}

正如您在代码末尾看到的那样,我正在记录线索和结果。以下是我在 error_log 文件中得到的内容。

[10-Apr-2015 15:08:34 UTC] PopCards Submission | Contact/Reserve Submission for John, Doe.
[10-Apr-2015 15:08:34 UTC] 
<?xml version="1.0" encoding="utf-8"?>  
<traffic        
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"      
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         
  contactdatetime="2015-04-10T15:08"        
  transactiondatetime="2015-04-10T15:08">   
    <trafficsource>     
      <vendorid>551d12d8a1de</vendorid>     
      <emailaddress></emailaddress>     
      <sourcename></sourcename>      
      <propertyname>CityView</propertyname> 
    </trafficsource>    
    <prospect>      
      <firstname>John</firstname>       
      <middlename></middlename>     
      <lastname>Doe</lastname>      
      <streetaddress1></streetaddress1>     
      <streetaddress2></streetaddress2>     
      <city></city>     
      <state></state>       
      <zipcode></zipcode>       
      <daytimephone></daytimephone>     
      <eveningphone></eveningphone>     
      <cellphone></cellphone>       
      <otherphone></otherphone>     
      <emailaddress>johndoe@gmail.com</emailaddress>        
      <comments></comments>   
    </prospect> 
    <prospectpreferences>       
      <pricerangemin></pricerangemin>       
      <pricerangemax></pricerangemax>       
      <numberofoccupants></numberofoccupants>       
      <pets></pets>     
      <dateneeded></dateneeded>     
      <appointmentdate></appointmentdate>       
      <appointmenttime></appointmenttime>       
      <numberofbedsdesired></numberofbedsdesired>       
      <numberofbathsdesired></numberofbathsdesired> 
    </prospectpreferences>  
  </traffic>


[10-Apr-2015 15:08:34 UTC] 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>500 - Internal server error.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>500 - Internal server error.</h2>
  <h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
 </fieldset></div>
</div>
</body>
</html>

我需要一些帮助/指导我在这里做错了什么。

【问题讨论】:

  • 我以前也做过同样的 SOAP 请求,标准的微软废话。如果您需要我的帮助并且您有我发布的 xml.log,请给我发消息。
  • @Misunderstood,我应该把 xml.log 文件放在哪里?我把它放在与包含上述函数的文件相同的目录中,xml.log文件中没有记录任何内容。
  • 那很好,没关系。我只需要看看它的内容。或者你可以echo "$responseHeader\n$info\n$result " 并粘贴到这里。
  • curl 很可能不是最适合这项工作的工具,如果您不想先了解所有 SOAP 内部知识,请使用标准的 SOAPClient。这正是您编写的文档中没有详细记录的部分:他们没有向您解释整个 SOAP 就是他们用这句话说的全部内容。
  • 在您的错误报告中至少应该提供 HTTP 状态代码。也许500?这通常意味着您搞砸了请求。首先尝试使用 SOAP 客户端使其工作,然后捕获工作请求的 HTTP 流量,然后可以使用 curl 重新构建。

标签: php xml wordpress curl gravity-forms-plugin


【解决方案1】:

好的,我们会一步一步来。

您的请求被运行该服务的 Serve 拒绝。

我们需要从 curl 获取您的请求标头。

将超时设置为 300,零 = 永远不会超时并且您没有收到任何响应,并且它会一直挂起,直到(如果)PHP 超时。

我希望您添加的这些选项都不会影响您的请求,它们仅用于测试和调试。

变化:

curl_setopt($ch, CURLOPT_HEADER, true);

添加这些:

curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_FAILONERROR,true);

添加此代码:

  $result = curl_exec($ch);
  if (curl_errno($ch)){
      $data .= 'Retreive Base Page Error: ' . curl_error($ch);
  }
  else {
    $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
    $responseHeader = substr($result ,0,$skip);
    $result = substr($result ,$skip);
    $info = var_export(curl_getinfo($ch),true);
   }
  $fp = fopen('xml.log','w');
  fwrite($fp,"$responseHeader\n$info\n$result ");
  fclose($fp);

发布此 xml.log 而不是您的日志。我认为这将包括你所拥有的以及更多。更多重要内容。


更新

变化:

      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));

收件人:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/xml'),'Content-Length: ' . strlen($xmlRequest ));

为什么会有这个?也许吧。

contactdatetime="$date" transactiondatetime="$date"

Heredoc 格式优于串联"' . $date . '"。太容易错过双引号或其他问题。

将命名空间(xmlns)全部放在一行上,不需要编辑器 EOL 问题。

 $xmlRequest = <<<EOX
<?xml version="1.0" encoding="utf-8"?>
<traffic xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" contactdatetime="$date" transactiondatetime="$date">
  <trafficsource>
    <vendorid>551d12d8a1de</vendorid>
    <emailaddress></emailaddress>
    <sourcename></sourcename>
    <propertyname>CityView</propertyname>
  </trafficsource>
  <prospect>
    <firstname>$fname</firstname>
    <middlename></middlename>
    <lastname>$lname</lastname>
    <streetaddress1></streetaddress1>
    <streetaddress2></streetaddress2>
    <city></city>
    <state></state>
    <zipcode></zipcode>
    <daytimephone></daytimephone>
    <eveningphone></eveningphone>
    <cellphone></cellphone>
    <otherphone></otherphone>
    <emailaddress>$userEmail</emailaddress>
    <comments>$comments</comments>
  </prospect>
  <prospectpreferences>
    <pricerangemin></pricerangemin>
    <pricerangemax></pricerangemax>
    <numberofoccupants></numberofoccupants>
    <pets></pets>
    <dateneeded></dateneeded>
    <appointmentdate></appointmentdate>
    <appointmenttime></appointmenttime>
    <numberofbedsdesired></numberofbedsdesired>
    <numberofbathsdesired></numberofbathsdesired>
  </prospectpreferences>
</traffic>
EOX;

结束更新 1


更新 2

我认为您的 XML 应该是这样的:

<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>
  <traffic xmlns="http://tempuri.org/PopCardInterfaceWebservice/Service1">
    <trafficsource>     
      <vendorid>551d12d8a1de</vendorid>     
      <emailaddress></emailaddress>     
      <sourcename></sourcename>      
      <propertyname>CityView</propertyname> 
    </trafficsource>    
    <prospect>      
      <firstname>John</firstname>       
      <middlename></middlename>     
      <lastname>Doe</lastname>      
      <streetaddress1></streetaddress1>     
      <streetaddress2></streetaddress2>     
      <city></city>     
      <state></state>       
      <zipcode></zipcode>       
      <daytimephone></daytimephone>     
      <eveningphone></eveningphone>     
      <cellphone></cellphone>       
      <otherphone></otherphone>     
      <emailaddress>johndoe@gmail.com</emailaddress>        
      <comments></comments>   
    </prospect> 
    <prospectpreferences>       
      <pricerangemin></pricerangemin>       
      <pricerangemax></pricerangemax>       
      <numberofoccupants></numberofoccupants>       
      <pets></pets>     
      <dateneeded></dateneeded>     
      <appointmentdate></appointmentdate>       
      <appointmenttime></appointmenttime>       
      <numberofbedsdesired></numberofbedsdesired>       
      <numberofbathsdesired></numberofbathsdesired> 
    </prospectpreferences>  
  </traffic>
 </soap:Body>
</soap:Envelope>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多