简体   繁体   English

创建不带WSDL文件的PHP SOAP请求(Flickr SOAP API)

[英]Create PHP SOAP request without a WSDL file (Flickr SOAP API)

I' trying to create a request for the Flickr SOAP API, but can't get the correct format. 我正在尝试为Flickr SOAP API创建一个请求,但是无法获取正确的格式。 Here is the XML they want sent.: 这是他们要发送的XML:

    <s:Envelope
    xmlns:s="http://www.w3.org/2003/05/soap-envelope"
    xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/1999/XMLSchema"
>
    <s:Body>
        <x:FlickrRequest xmlns:x="urn:flickr">
            <method>flickr.test.echo</method>
            <name>value</name>
        </x:FlickrRequest>
    </s:Body>
</s:Envelope>

Here is my code: 这是我的代码:

<?php
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$opts = array('location' => 'http://api.flickr.com/services/soap/',
              'uri'      => 'urn:flickr',
              'trace'    => 1
);
$client = new SOAPClient(null, $opts);
?>

    <?php
      try {

        $data = $client->__soapCall('flickr.test.echo', array('name'));
        print_r($data);

      } catch (SoapFault $exception) {

        echo 'Exception Thrown: '.$exception->faultstring.'<br><br>';  

      print "<pre>\n";
      print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
      print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
      print "</pre>";   

      }

    ?>

Here is the response: 这是响应:

Exception Thrown: 

Request :
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns1="urn:flickr" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:flickr.test.echo>
<param0 xsi:type="xsd:string">name</param0>
</ns1:flickr.test.echo>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Response:
<?xml version="1.0" encoding="utf-8" ?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
    <s:Body>
        <s:Fault>
            <faultcode>flickr.error.0</faultcode>
            <faultstring>Invalid SOAP envelope.</faultstring>
            <faultactor>http://www.flickr.com/services/soap/</faultactor>
            <details>Please see http://www.flickr.com/services/api/ for more details</details>
        </s:Fault>
    </s:Body>
</s:Envelope>

Unfortunately Flickr does not provide a WSDL file. 不幸的是,Flickr没有提供WSDL文件。

thanks 谢谢

<< ** UPDATE ** >> I'm getting closer. << **更新** >>我越来越近了。 I changed my code and it's almost to where it's supposed to be: 我更改了代码,几乎到了应该的位置:

<?php
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache

$opts = array('location' => 'http://api.flickr.com/services/soap/',
              'uri'      => 'http://api.flickr.com/services/soap/',
              'trace'    => 1
);
$client = new SOAPClient(null, $opts);
?>
<?php
  try {

    $data = $client->__soapCall("FlickrRequest",
       array(new SoapParam('flickr.test.echo', 'method'), new SoapParam('value', 'name')),
       array('soapaction' => 'http://api.flickr.com/services/soap/')
    );  
    print_r($data);


  } catch (SoapFault $exception) {

    echo 'Exception Thrown:  '.$exception->faultstring.'<br><br>';  

  print "<pre>\n";
  print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
  print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
  print "</pre>";   
  }

?>

What's being sent now is: 现在发送的是:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://api.flickr.com/services/soap/" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:FlickrRequest>
<method xsi:type="xsd:string">flickr.test.echo</method>
<name xsi:type="xsd:string">value</name>
</ns1:FlickrRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I managed to make it work using this code: 我设法使用以下代码使其工作:

<?php 
    $url = 'https://api.flickr.com/services/soap';

    $xml_post_string = '<s:Envelope
                            xmlns:s="http://www.w3.org/2003/05/soap-envelope"
                            xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
                            xmlns:xsd="http://www.w3.org/1999/XMLSchema"
                        >
                            <s:Body>
                                <x:FlickrRequest xmlns:x="urn:flickr">
                                    <method>flickr.photos.getRecent</method>
                                    <api_key>YOUR_API_KEY</api_key>
                                    <page>1</page>
                                    <per_page>10</per_page>
                                </x:FlickrRequest>
                            </s:Body>
                        </s:Envelope>';

       $headers = array(
                    "Content-type: text/xml;charset=\"utf-8\"",
                    "Accept: text/xml",
                    "Cache-Control: no-cache",
                    "Pragma: no-cache",
                    "Content-length: ".strlen($xml_post_string),
                );

        // PHP cURL
        $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);

        $response = curl_exec($ch);
        curl_close($ch);
?>

The method that I call is flickr.photos.getRecent , and the parameters passed are: 我调用的方法是flickr.photos.getRecent ,传递的参数是:

  • api_key --> tour api key api_key- >游览api密钥
  • page --> the page targeted 页面 ->目标页面
  • per_page --> the number of elements per page per_page- >每页元素数

Here is the response: 这是响应:

    <s:envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema">
    <s:body>
    <x:flickrresponse xmlns:x="urn:flickr">
&lt;photos page="1" pages="100" perpage="10" total="1000"&gt;
    &lt;photo id="14569264388" owner="14957549@N05" secret="6edf4e63b7" server="2924" farm="3" title="Halona Blow Hole.  #hawaii #oahu #halonablowhole #honolulu #beautifulscenery #southshore" ispublic="1" isfriend="0" isfamily="0" /&gt;
    &lt;photo id="14569264418" owner="107442773@N05" secret="5260e4a304" server="3877" farm="4" title="Chic Planner’s selection of flowers is rich and varied. The arrangements, which were also personally selected by Mr.Suriya Krutthong Creative Director of Chic Planner's, are desinged of contemporary and exude a sense of timeless elegance.  #Floral #ChicPl" ispublic="1" isfriend="0" isfamily="0" /&gt;
    &lt;photo id="14569463647" owner="88628573@N00" secret="9499a95683" server="2897" farm="3" title="DSC01138.JPG" ispublic="1" isfriend="0" isfamily="0" /&gt;
    &lt;photo id="14569464207" owner="13792595@N04" secret="6a3ef91dc1" server="3902" farm="4" title="2014-07-25 16.59.40" ispublic="1" isfriend="0" isfamily="0" /&gt;
    &lt;photo id="14569464337" owner="84797770@N05" secret="5749d84b11" server="2930" farm="3" title="PICT0340.JPG" ispublic="1" isfriend="0" isfamily="0" /&gt;
    &lt;photo id="14732900256" owner="112321297@N06" secret="a42fc5a063" server="3904" farm="4" title="005" ispublic="1" isfriend="0" isfamily="0" /&gt;
    &lt;photo id="14753554624" owner="126377022@N07" secret="daf6c95548" server="3900" farm="4" title="Image from page 36 of &amp;quot;Illustrated catalogue and price list of bells, electric gas burners, batteries, push buttons ...&amp;quot; (1899)" ispublic="1" isfriend="0" isfamily="0" /&gt;
    &lt;photo id="14775778843" owner="34395619@N04" secret="b5f2702bec" server="5585" farm="6" title="_MG_7248.jpg" ispublic="1" isfriend="0" isfamily="0" /&gt;
    &lt;photo id="14775779293" owner="67793058@N00" secret="447ebe21d3" server="2900" farm="3" title="IMG_1722" ispublic="1" isfriend="0" isfamily="0" /&gt;
    &lt;photo id="14775779583" owner="124068747@N04" secret="38fd7024d8" server="3860" farm="4" title="P7270139" ispublic="1" isfriend="0" isfamily="0" /&gt;
&lt;/photos&gt;
        </x:flickrresponse>
    </s:body>
</s:envelope>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM