php PHP4:通过 cURL 通过 HTTPS/POST 发送 XML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/238784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP4: Send XML over HTTPS/POST via cURL?
提问by starmonkey
I wrote a class/function to send xml over https via PHP4/cURL, just wondering if this is the correct approach, or if there's a better one.
我写了一个类/函数来通过 PHP4/cURL 通过 https 发送 xml,只是想知道这是否是正确的方法,或者是否有更好的方法。
Note that PHP5 is not an option at present.
请注意,PHP5 目前不是一个选项。
/**
* Send XML via http(s) post
*
* curl --header "Content-Type: text/xml" --data "<?xml version="1.0"?>...." http://www.foo.com/
*
*/
function sendXmlOverPost($url, $xml) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// For xml, change the content-type.
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // ask for results to be returned
if(CurlHelper::checkHttpsURL($url)) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
}
// Send to remote and return data to caller.
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
cheers!
干杯!
回答by jakber
If the protocol you are using is XML-RPC (looks like it based on what you said) and you are using at least PHP 4.2, have a look at http://phpxmlrpc.sourceforge.net/for libraries and resources.
如果您使用的协议是 XML-RPC(根据您所说的看起来像它)并且您至少使用 PHP 4.2,请查看http://phpxmlrpc.sourceforge.net/以获取库和资源。
回答by jhon
Great solution! Found a similar one here also:
很棒的解决方案!在这里也找到了一个类似的:
Also they have showed how to receive this kind of XML/JSON on server
他们还展示了如何在服务器上接收这种 XML/JSON
// here you can have all the required business checks
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){
$postText = file_get_contents('php://input');
}
回答by Dr. Rajesh Rolen
$ch = curl_init($serviceUrl);
$ch = curl_init($serviceUrl);
if( $this -> usingHTTPS() )
{
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->sslVerifyHost);
}
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "OTA_request=".urlencode($this->xmlMessage));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$this->xmlResponse = curl_exec ($ch);
$this -> callerFactory -> dbgMsg('xmlResponse: <hr><pre>'.htmlentities($this->xmlResponse).'</pre><hr>'. curl_error($ch));
curl_close ($ch);
$this->checkResponse();
回答by Nick Stinemates
Use the SoapClientclass provided with most PHP installations
使用大多数 PHP 安装提供的SoapClient类
An example is:
一个例子是:
$soap = new SoapClient("http://some.url/service/some.wsdl");
$args = array("someTypeName" => "someTypeValue"
"someOtherTypeName" => "someOtherTypeValue");
$response = $soap->executeSomeService($args);
print_r($response);

