通过cURL访问函数/方法


Accessing function/method through 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)

如果下载并使用SoapClientTimeout,然后简单地更改类的__doRequest方法,并包含$request变量的转储,以了解SoapClient如何包装它:

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

并将其称为常规soap客户端:

$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