2013-01-14 125 views
0

我试图为Flickr SOAP API创建请求,但无法获得正确的格式。 这里就是他们想要发送的XML:创建没有WSDL文件的PHP SOAP请求(Flickr SOAP API)

<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> 

这里是我的代码:

<?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>"; 

     } 

    ?> 

这里是回应:

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> 

不幸的Flickr不提供WSDL文件。

感谢

< < **更新** >> 我越来越近。我改变了我的代码,它是几乎到了它应该是:

<?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>"; 
    } 

?> 

什么是现在正在发送的是:

<?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> 

回答

0

我设法使它工作使用此代码:

<?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); 
?> 

我调用的方法是flickr.photos.getRecent,传递的参数是:

  • API_KEY - >旅游API密钥
  • - >页面针对性
  • per_page - >每页

这里元素的数量是响应:

<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="[email protected]" 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="[email protected]" 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="[email protected]" secret="9499a95683" server="2897" farm="3" title="DSC01138.JPG" ispublic="1" isfriend="0" isfamily="0" /&gt; 
    &lt;photo id="14569464207" owner="[email protected]" 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="[email protected]" secret="5749d84b11" server="2930" farm="3" title="PICT0340.JPG" ispublic="1" isfriend="0" isfamily="0" /&gt; 
    &lt;photo id="14732900256" owner="[email protected]" secret="a42fc5a063" server="3904" farm="4" title="005" ispublic="1" isfriend="0" isfamily="0" /&gt; 
    &lt;photo id="14753554624" owner="[email protected]" 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="[email protected]" secret="b5f2702bec" server="5585" farm="6" title="_MG_7248.jpg" ispublic="1" isfriend="0" isfamily="0" /&gt; 
    &lt;photo id="14775779293" owner="[email protected]" secret="447ebe21d3" server="2900" farm="3" title="IMG_1722" ispublic="1" isfriend="0" isfamily="0" /&gt; 
    &lt;photo id="14775779583" owner="[email protected]" secret="38fd7024d8" server="3860" farm="4" title="P7270139" ispublic="1" isfriend="0" isfamily="0" /&gt; 
&lt;/photos&gt; 
     </x:flickrresponse> 
    </s:body> 
</s:envelope>