PHP SOAP HTTP Request

后端 未结 3 539
心在旅途
心在旅途 2020-12-10 07:20

Despite being a PHP developer for a while, I\'m just now getting my first taste of web services. I was hoping to get a little help, as the book I am using is not much help.

相关标签:
3条回答
  • 2020-12-10 07:50

    Well this is definitely a SOAP request, so you'll need to use SOAP to work correctly with this or you are in for a major headache.

    I've had several encounter with SOAP and PHP and everytime i had to rely on an external library. The most recent one i had to use was Zend_Soap_Client.

    But then again, do you have the WSDL available? You need a WSDL to be able to use a SOAP webservice using a client library.

    http://framework.zend.com/manual/en/zend.soap.html

    And here is a sample of my code i used, i hope it'll get you started

    ini_set('soap.wsdl_cache_enabled', 0);
    set_include_path(dirname(__FILE__).'/../../libraries/:'.get_include_path());
    require_once 'Zend/Loader/Autoloader.php';
    Zend_Loader_Autoloader::getInstance();
    
    //Include the classes for the webservice
    include('CatalogOrder.php');
    include('CatalogOrderItem.php');
    include('CatalogOrderWebservice.php');
    
    //Check the mode
    if(isset($_GET['wsdl'])) {
    
        $autodiscover = new Zend_Soap_AutoDiscover(array(
            'classmap' => array(
                'CatalogOrder' => "CatalogOrder",
                'CatalogOrderItem' => "CatalogOrderItem"
            )
        ));
        $autodiscover->setComplexTypeStrategy(new Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex());
        $autodiscover->setClass('CatalogOrderWebService');
        $autodiscover->handle();
    
    //Return the consume form and process the actions of the consumer
    } elseif(isset($_GET['consume'])) {
    
        // pointing to the current file here
        $soap = new Zend_Soap_Client("http://".$_SERVER['HTTP_HOST']."/admin/export/WebService.php?wsdl", array(
            'classmap' => array(
                'CatalogOrder' => "CatalogOrder",
                'CatalogOrderItem' => "CatalogOrderItem"
            ),
            'encoding' => 'iso-8859-1'
        ));
        include('CatalogOrderWebserviceConsumer.php');
    
    //Process SOAP requests
    } else {
    
        // pointing to the current file here
        $soap = new Zend_Soap_Server("http://".$_SERVER['HTTP_HOST']."/admin/export/WebService.php?wsdl", array(
            'classmap' => array(
                'CatalogOrder' => "CatalogOrder",
                'CatalogOrderItem' => "CatalogOrderItem"
            ),
            'encoding' => 'iso-8859-1'
        ));
        $soap->setClass('CatalogOrderWebService');
        $soap->handle();
    
    }
    
    0 讨论(0)
  • 2020-12-10 07:53

    As the provider of the service, the other company should supply you with a WSDL document which describes the service in computer readable terms. Typically they are provided via an url like http://their.service.url/wsdl or similar.

    Once you have that you should be able to create a SoapClient instance to interact with the service.

    0 讨论(0)
  • 2020-12-10 07:59

    You have a couple of options! You could use soap objects to create the request which, based upon a WSDL will know the correct way to talk to the remote server. You can see how to do this at the PHP manual.

    Alternatively, you can use CURL to do the work. You'll need to know where to post the data to (which it looks like is in the example above), then you can just do something like this:

    $curlData = "<?xml version="1.0" encoding="utf-8"?>... etc";
    $url='http://wherever.com/service/';
    $curl = curl_init();
    
    curl_setopt ($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl,CURLOPT_TIMEOUT,120);
    curl_setopt($curl,CURLOPT_ENCODING,'gzip');
    
    curl_setopt($curl,CURLOPT_HTTPHEADER,array (
        'SOAPAction:""',
        'Content-Type: text/xml; charset=utf-8',
    ));
    
    curl_setopt ($curl, CURLOPT_POST, 1);
    curl_setopt ($curl, CURLOPT_POSTFIELDS, $curlData);
    
    $result = curl_exec($curl);
    curl_close ($curl);
    

    You then should have the result in the $result var. You can then try to convert it to an XML doc, although sometimes I've found due to encoding this doesn't work:

    $xml = new SimpleXMLElement($result);
    print_r($xml);
    
    0 讨论(0)
提交回复
热议问题