PHP4: отправлять XML через HTTPS / POST через cURL? - PullRequest
7 голосов
/ 27 октября 2008

Я написал класс / функцию для отправки xml через https через PHP4 / cURL, просто задаваясь вопросом, правильный ли это подход или есть лучший.

Обратите внимание, что 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;
}

ура!

Ответы [ 4 ]

4 голосов
/ 29 ноября 2011

Отличное решение! Нашел похожий здесь также:

Также они показали, как получить этот вид XML / JSON на сервере

// here you can have all the required business checks
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){
    $postText = file_get_contents('php://input');
}
4 голосов
/ 27 октября 2008

Если вы используете протокол XML-RPC (похоже, он основан на том, что вы сказали) и вы используете хотя бы PHP 4.2, посмотрите http://phpxmlrpc.sourceforge.net/ для библиотек и ресурсов.

3 голосов
/ 29 декабря 2009

$ ch = curl_init ($ serviceUrl);

<code>    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).'

». curl_error ($ ч)); curl_close ($ ch); $ This-> checkResponse ();
0 голосов
/ 27 октября 2008

Используйте класс SoapClient , предоставляемый большинством установок PHP

Пример:

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

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

print_r($response);
...