PHP4:通过 cURL 通过 HTTPS/POST 发送 XML?

发布于 2024-07-07 04:57:06 字数 926 浏览 7 评论 0原文

我编写了一个类/函数来通过 PHP4/cURL 通过 https 发送 xml,只是想知道这是否是正确的方法,或者是否有更好的方法。

请注意,目前 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;
}

干杯!

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.

Note that PHP5 is not an option at present.

/**
 * 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

七秒鱼° 2024-07-14 04:57:06

很好的解决方案! 在这里也找到了类似的:

他们还展示了如何在服务器上接收这种XML/JSON

// here you can have all the required business checks
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){
    $postText = file_get_contents('php://input');
}

Great solution! Found a similar one here also:

Also they have showed how to receive this kind of XML/JSON on server

// here you can have all the required business checks
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){
    $postText = file_get_contents('php://input');
}
你是年少的欢喜 2024-07-14 04:57:06

如果您使用的协议是 XML-RPC(看起来像您所说的)并且您至少使用 PHP 4.2,请查看 http://phpxmlrpc.sourceforge.net/ 用于库和资源。

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.

极致的悲 2024-07-14 04:57:06

$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();

$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();
司马昭之心 2024-07-14 04:57:06

使用大多数 PHP 安装提供的 SoapClient

类例子是:

$soap = new SoapClient("http://some.url/service/some.wsdl");
$args = array("someTypeName" => "someTypeValue"
              "someOtherTypeName" => "someOtherTypeValue");

$response = $soap->executeSomeService($args);

print_r($response);

Use the SoapClient class provided with most PHP installations

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文