将表单数据发送到SOAP API,无法获得--soapCall的格式

时间:2018-02-18 18:59:09

标签: php arrays xml soap

我正在使用php表单将数据发送到CRM以收集数据并将其放在CRM上的正确位置以通知公司中的正确部门。其他人编写了API来收集信息,他们为我提供了SOAP URL。他们向我提供了示例XML,该示例XML已在SOAPUI中工作,以提供正确的GUID响应。

我编写了PHP来从表单中收集数据,将其放在用于在SOAP API中测试API的相同xml代码中。我很难获得正确的提交代码。我的问题类似于此处的页面:How do I send XML data to an API via SOAP in PHP?。但是使用$ client->提交给我一个错误,说这不是一个函数。 所以我回到了SOAP 1.2规范并尝试了__soapCall(function(array))。 我没有得到回应。我尝试过其他一些东西,但这似乎应该可行。

要发送的xml示例:

    <soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
            <soapenv:header>
            <soapenv:body>
                <tem:addnewlead>
                    <!--Optional-->
                    <tem:firstname>test</tem:firstname>
                    <!--Optional-->
                    <tem:lastname>test2</tem:lastname>
                    <!--Optional-->
                    <tem:emailaddress>test@test.com</tem:emailaddress>
                    <!--Optional-->
                    <tem:mainphone>555-555-5555</tem:mainphone>
                    <!--Optional-->
                    <tem:companyname>testtest</tem:companyname>
                    <!--Optional-->
                    <tem:interest>testtesttest</tem:interest>
                    <!--Optional-->
                    <tem:requesttype>test</tem:requesttype>
                    <!--Optional-->
                    <tem:requestdetails>testtesttest</tem:requestdetails>
                </tem:addnewlead>
            </soapenv:body>
        </soapenv:header>
 </soapenv:envelope>

这是带有代码的php,为了简洁而删除了xml,因为我知道这是有效的,使用上面的xml发送它与之前的编码器在SOAPUI中的测试匹配:

    <?php 
         $URL = 'http://zzzzzzzz.com/APIService.svc';
         $send = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
            <soapenv:Header/>
            <soapenv:Body>
                <tem:ADDNEWLEAD>
                    <!--Optional-->
                    <tem:FirstName>test</tem:FirstName>
                    <!--Optional-->
                    <tem:LastName>test2</tem:LastName>
                    <!--Optional-->
                    <tem:emailAddress>test@test.com</tem:emailAddress>
                    <!--Optional-->
                    <tem:MainPhone>555-555-5555</tem:MainPhone>
                    <!--Optional-->
                    <tem:CompanyName>testtest</tem:CompanyName>
                    <!--Optional-->
                    <tem:Interest>testtesttest</tem:Interest>
                    <!--Optional-->
                    <tem:RequestType>test</tem:RequestType>
                    <!--Optional-->
                    <tem:RequestDetails>testtesttest</tem:RequestDetails>
                </tem:ADDNEWLEAD>
            </soapenv:Body>
        </soapenv:Envelope>";
      print_r($send);
     try {
         $client = new SoapClient("http://zzzzzzzz.com/APIService.svc?WSDL",array(
        'location'      => $URL,
        'uri'           => "http://zzzzzzz.com/",
        'trace'         => 1,
        'exceptions'    => true
    ));
    $return = $client->__soapCall(array('ADDNEWLEAD' => $send));
    $functions = $client->__getFunctions();
    // Some other code that I thought I would try as I am not the best SOAP API writer as you can tell by now.
    //  $return = $client->__soapCall(array(DoWork $xml, ADDNEWLEAD $send));
    //  $return = $client->DoWork(ADDNEWLEAD($send));
   //   $return = $client->__soapCall('DoWork',array('DoWork' => null,'ADDNEWLEAD' => $send));
    echo '<pre>'; //show response on screen
     echo '<br>-';
   print_r($functions);
     echo '<br>+';
   print_r($return);
   echo '</pre>';
   } catch(SoapFault $e) {
    echo '<h3>Exception</h3>';
    echo '<pre>';
   print_r($e);
   echo '</pre>';
  }
  die(); ?>

我明白了:

   Array
   (
     [0] => DoWorkResponse DoWork(DoWork $parameters)
     [1] => ADDNEWLEADResponse ADDNEWLEAD(ADDNEWLEAD $parameters)
   )
   stdClass Object (
        [ADDNEWLEADResult] =>  )

如果它是黑色的,我应该从API获得确认码。

任何帮助表示赞赏。 谢谢。

当提交无效时,我也收到了这个xml响应:

  

            /**

            * Class GF_Services_API

            *

            * Accesses the API for Service Request data

            *

            * @since 1.0.0

            */



            // Exit if accessed directly

            if ( ! defined( 'ABSPATH' ) ) exit;



            ini_set('default_socket_timeout', 120);



            class GF_Services_API {



                   /**

                   * @var               GF_Services_API $endpoint The Endpoint that we ping for data

                   * @since             1.0.0

                   */

                   private $endpoint = 'http://xxxxxxxxxxxxxxxx/Alpha.svc?wsdl';



                   /**

                   * GF_Services_API constructor.

                   *

                   * @since 1.0.0

                   */

                   function __construct() {

                   }



                   /**

                   * I guess this is marginally easier than using Capture Groups in Regex...?

                   * Maybe it is more efficient, don't feel like running a benchmark. This is how it was to begin with.

                   * 

                    * @param             string $haystack Input to Search

                   * @param             string $start    Opening Tag

                   * @param             string $end      Closing Tag

                   *              

                    * @access            private

                   * @since             1.0.0

                   * @return            string String between the opening and closing Tag

                   */

                   private function substring_between( $haystack, $start, $end ) {



                           if ( strpos( $haystack, $start ) === false ||

                                  strpos( $haystack, $end ) === false) {

                                  return false;

                           }

                           else {



                                  $start_position = strpos( $haystack, $start ) + strlen( $start );

                                  $end_position = strpos( $haystack, $end );



                                  return substr( $haystack, $start_position, $end_position - $start_position );



                           }



                   }





                   /**

                   * Grab Information from the API using a Ticket Number

                   * 

                    * @param             string $ticket_number Ticket Number

                   *                                      

                    * @access            public

                   * @since             1.0.0

                   * @return            array  API Data for the Ticket Number

                   */

                   public function get_information_by_ticket_number( $ticket_number = '' ) {



                           $return_array = array();



                           if ( $ticket_number != '' ) {



                                  try {



                                          $wsdl = $this->endpoint;



                                          // options for OpenSSL in php 5.6.5

                                          $opts = array(

                                                 'ssl' => array(

                                                        'ciphers' => 'RC4-SHA',

                                                        'verify_peer' => false,

                                                        'verify_peer_name' => false

                                                 )

                                          );



                                          // SOAP 1.2 client

                                          $params = array(

                                                 'encoding' => 'UTF-8',

                                                 'verifypeer' => false,

                                                 'verifyhost' => false,

                                                 //'soap_version' => SOAP_1_2, SOAP v1.2 doesn't support text/html which we need

                                                 'trace' => 1,

                                                 'exceptions' => 1,

                                                 'connection_timeout' => 180,

                                                 'stream_context' => stream_context_create( $opts )

                                          );



                                          $cliente = new SoapClient( $wsdl, $params );



                                          $data = array(

                                                 "TicketNo" => $ticket_number,

                                                 "Key1" => '76xxxxxxxxxxxxxx94',

                                                 "Key2" => '6x-xxxxxxxxxxxxxxx4xxxxxxxxxxxxx16'

                                          );



                                          $result = $cliente->__call( 'CheckServiceTicket', array( $data ) );

                                          $result_data = $result->CheckServiceTicketResult->any;



                                         $return_array['serial_number'] = $this->substring_between( $result_data, '<serial_no>', '</serial_no>' );

                                         $return_array['descr'] = $this->substring_between( $result_data, '<descr>', '</descr>' );

                                         $return_array['contract_id'] = $this->substring_between( $result_data, '<Contract_ID>', '</Contract_ID>' );

                                         $return_array['contract_status'] = $this->substring_between( $result_data, '<Contract_Status>', '</Contract_Status>' );

                                         $return_array['contract_expiry_date'] = $this->substring_between( $result_data, '<Contract_Expiry_date>', '</Contract_Expiry_date>' );

                                         $return_array['address_1'] = $this->substring_between( $result_data, '<address_1>','</address_1>');

                                         $return_array['address_2'] = $this->substring_between( $result_data, '<address_2>', '</address_2>' );

                                         $return_array['address_3'] = $this->substring_between( $result_data, '<address_3>', '</address_3>' );

                                         $return_array['city'] = $this->substring_between( $result_data, '<city>', '</city>' );

                                         $return_array['state'] = $this->substring_between( $result_data, '<state>', '</state>' );

                                         $return_array['zip'] = $this->substring_between( $result_data, '<zip>', '</zip>' );

                                         $return_array['country'] = $this->substring_between( $result_data, '<country>', '</country>' );

                                         $return_array['company_name'] = $this->substring_between( $result_data, '<Company_Name>', '</Company_Name>' );

                                         $return_array['device_type'] = $this->substring_between( $result_data, '<Device_Type>', '</Device_Type>' );

                                         $return_array['state'] = $this->get_us_state_name( $return_array['state'] );

                                         $return_array['contract_expiry_date'] = date( "F d, Y", strtotime( $return_array['contract_expiry_date'] ) );



                                  }

                                  catch ( Exception $e ) {

                                          echo "Error: " . $e->getMessage();

                                  }



                           }



                           return $return_array;



                   }



                   /**

                   * Gets an Array of Service Requests Information (Including Status) via an Email Address

                   * 

                    * @param             string $email Email Address

                   *                             

                    * @access            public

                   * @since             1.0.0

                   * @return            array  Array of Service Request Info

                   */

                   public function get_information_by_email_address( $email = '' ) {



                           $return_array = array();



                           if ( $email != '' ) {



                                  try {



                                          $wsdl = $this->endpoint;



                                          // options for OpenSSL in php 5.6.5

                                          $opts = array(

                                                 'ssl' => array(

                                                        'ciphers' => 'RC4-SHA',

                                                        'verify_peer' => false,

                                                        'verify_peer_name' => false

                                                 )

                                          );



                                          // SOAP 1.2 client

                                          $params = array(

                                                 'encoding' => 'UTF-8',

                                                 'verifypeer' => false,

                                                 'verifyhost' => false,

                                                 //'soap_version' => SOAP_1_2, SOAP v1.2 doesn't support text/html which we need

                                                 'trace' => 1,

                                                 'exceptions' => 1,

                                                 'connection_timeout' => 180,

                                                 'stream_context' => stream_context_create( $opts )

                                          );



                                          $client = new SoapClient( $wsdl, $params );



                                          $data = array(

                                                 "Email" => $email,

                                                 "Key1" => '7x',

xxxxxxxxxxxxxxxxxxxxxxxxxxx                                                      &#34;密钥2&#34; =&GT; &#39; 6xxxxxxxxxxxxxxxxxxxxxx&#39;

                                          );



                                          $result = $client->__call( 'QueryTicketInfo', array( $data ) );



                                          $xml = $result->QueryTicketInfoResult->any;



                                          // Remove namespaces

                                          $xml = str_replace( array( "diffgr:", "msdata:" ), '', $xml );

                                          $xml = "<package>" . $xml . "</package>";

                                          $data = simplexml_load_string( $xml );

                                          $tickets = $data->diffgram->DocumentElement->WS0002;



                                          foreach ( $tickets as $ticket ) {



                                                 $_result = array();

                                                 $_result['Opened'] = (string) $ticket->Opened;

                                                 $_result['LastAction'] = (string) $ticket->LastAction;

                                                 $_result['Enteredby'] = (string) $ticket->Enteredby;

                                                 $_result['TicketNumber'] = (string) $ticket->TicketNumber;

                                                 $_result['ProblemDescription'] = (string) $ticket->ProblemDescription;

                                                 $_result['CurrentStatus'] = (string) $ticket->CurrentStatus;

                                                 $_result['CustomerName'] = (string) $ticket->CustomerName;

                                                 $_result['CalledInBy'] = (string) $ticket->CalledInBy;

                                                 $_result['Email'] = (string) $ticket->Email;

                                                 $_result['ServiceAgent'] = (string) $ticket->ServiceAgent;

                                                 $_result['PartNumber'] = (string) $ticket->PartNumber;

                                                 $_result['PartDescription'] = (string) $ticket->PartDescription;

                                                 $_result['SerialNumber'] = (string) $ticket->SerialNumber;

                                                 $_result['City'] = (string) $ticket->City;

                                                 $_result['State'] = (string) $ticket->State;

                                                 $_result['LastActivity'] = (string) $ticket->LastActivity;

                                                 $_result['DO'] = (string) $ticket->DO;



                                                 $return_array[] = $_result;



                                          }



                                  }

                                  catch ( Exception $e ) {

                                          echo "Error: " . $e->getMessage();

                                  }



                           }



                           return $return_array;



                   }



            }

1 个答案:

答案 0 :(得分:1)

使用PHP SoapClient时,您不需要直接使用XML。构建PHP请求对象:

$addNewLeadRequest = new \stdClass();
$addNewLeadRequest->FirstName = 'test';
$addNewLeadRequest->LastName = 'test2';
$addNewLeadRequest->emailAddress = 'test@test.com';
$addNewLeadRequest->MainPhone = '555-555-5555';
$addNewLeadRequest->CompanyName = 'testtest';
$addNewLeadRequest->Interest = 'testtesttest';
$addNewLeadRequest->RequestType = 'test';
$addNewLeadRequest->RequestDetails = 'testtesttest';

然后将其传递给客户端上的方法:

$addNewLeadResponse = $client->ADDNEWLEAD($addNewLeadRequest);