通过cURL访问功能/方法

时间:2014-06-02 05:54:24

标签: php curl

如果我使用SoapClient,我的代码将如下所示:

$baseurl = 'http://www.webservicex.net/geoipservice.asmx?WSDL';
$client = new SoapClient($baseurl);
print_r($client->GetGeoIP(array('IPAddress'=>'123.45.67.890')));

因此,使用$client,我可以访问GetGeoIP方法。如何使用cURL做同样的事情?

到目前为止,我已将代码设置为:

$curl = curl_init($baseurl);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$rawPOSTdata = array(
    "IPAddress"     =>  "123.45.67.890",    
    );
// now how do I specify that I want to send $rawPOSTdata to the GetGeoIP() function?
curl_setopt($curl, CURLOPT_POSTFIELDS, $rawPOSTdata);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));      

$response = curl_exec($curl);

现在我在哪里以及如何指定函数调用GetGeoIP

修改

扩展SOAP类之后,这就是我为$request

所获得的
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.webservicex.net/">
   <SOAP-ENV:Body>
      <ns1:GetGeoIP>
         <ns1:IPAddress>123.45.67.890</ns1:IPAddress>
      </ns1:GetGeoIP>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

所以,如果我做对了,我应该将它存储在一个变量中并在

中使用

curl_setopt($curl, CURLOPT_POSTFIELDS, $xml_string)

1 个答案:

答案 0 :(得分:0)

如果您下载并使用SoapClientTimeout,然后只需更改班级__doRequest方法,并包含$request变量的转储,以确定SoapClient的方式包裹它:

public function __doRequest($request, $location, $action, $version, $one_way = FALSE) {
    echo htmlentities($request);
    //...
}

并将其称为常规肥皂客户端:

$baseurl = 'http://www.webservicex.net/geoipservice.asmx?WSDL';
$client = new SoapClientTimeout($baseurl);
print_r($client->GetGeoIP(array('IPAddress'=>'123.45.67.890')));

..你会得到答案:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.webservicex.net/">
   <SOAP-ENV:Body>
      <ns1:GetGeoIP>
         <ns1:IPAddress>123.45.67.890</ns1:IPAddress>
      </ns1:GetGeoIP>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

整个XML字符串进入CURLOPT_POSTFIELDS