PHP4: Send XML over HTTPS/POST via cURL?
Asked Answered
M

4

7

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!

Macadamia answered 26/10, 2008 at 23:19 Comment(1)
Wow, not an option at present? :( Also, if you are not set on cURL, look into HTTP_Request (pear.php.net/package/HTTP_Request).Antrim
B
4

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');
}
Buckler answered 29/11, 2011 at 17:13 Comment(0)
P
3

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.

Pestilent answered 27/10, 2008 at 14:34 Comment(0)
J
3

$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();
Jesusitajet answered 29/12, 2009 at 10:31 Comment(0)
C
-1

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);
Curvy answered 26/10, 2008 at 23:27 Comment(1)
Cheers, but I don't think the XML API I'm using actually uses SOAP, just reads a string from a post.Macadamia

© 2022 - 2024 — McMap. All rights reserved.