PHP4:通过HTTPS / POST通过cURL发送XML? [英] PHP4: Send XML over HTTPS/POST via cURL?

查看:452
本文介绍了PHP4:通过HTTPS / POST通过cURL发送XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

注意,PHP5目前不是一个选项。

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;
}

干杯!

推荐答案

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

Great solution! Found a similar one here also:

  • Post JSON/XML data with curl and receive it on other end

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

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');
}

这篇关于PHP4:通过HTTPS / POST通过cURL发送XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆