【问题标题】:PHP4: Send XML over HTTPS/POST via cURL?PHP4:通过 cURL 通过 HTTPS/POST 发送 XML?
【发布时间】:2010-09-19 07:43:26
【问题描述】:

我编写了一个类/函数来通过 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;
}

干杯!

【问题讨论】:

标签: php xml curl https php4


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        一个例子是:

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

        【讨论】:

        • 干杯,但我不认为我使用的 XML API 实际上使用 SOAP,只是从帖子中读取一个字符串。
        猜你喜欢
        • 2011-10-24
        • 2013-04-02
        • 1970-01-01
        • 2014-03-12
        • 2012-03-04
        • 1970-01-01
        • 1970-01-01
        • 2018-01-03
        • 2016-02-25
        相关资源
        最近更新 更多