PHP SOAP HTTP Request

后端 未结 3 540
心在旅途
心在旅途 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: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 = "... 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);
    

提交回复
热议问题