0

我正在尝试为 Flickr SOAP API 创建请求,但无法获得正确的格式。这是他们想要发送的 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>

这是我的代码:

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

1 回答 1

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 密钥
  • page --> 目标页面
  • 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="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>
于 2014-07-27T10:18:35.030 回答