Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send an XML post request to a web server with CURL

Tags:

php

curl

wms

I am trying to send a request to a web server using php and curl. I haven't done something like this before and although there are many nice examples online I have some difficulties understanding some of the curl commands.

This is what I want to do: There is an established web service (for example: Web map service) and I want my php code to send a post XML request to this service. As a respond I want to get an XML file.

This is what I have till now:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, ''); 
    /*curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));*/
    /* curl_setopt($ch, CURLOPT_HEADER, 0);*/
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    /*curl_setopt($ch, CURLOPT_REFERER, '');*/
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $ch_result = curl_exec($ch);
    curl_close($ch);
    echo $ch_result;

As I said I am quite new in php and also in using curl and I think I am missing some concepts. My questions are: 1) What is the string (link) that I have to put in the:

          curl_setopt($ch, CURLOPT_URL, ''); 

Is it the host name of the service which I want to send the request?

2) In row 6 the variable $xml contains the xml file that I want to send as a request. Is it correct or this variable is supposed to contain something else?

3) In which cases do I need to use a httpheader or header (row3 and row4);

Thanks for the help. Dimitris

like image 616
user1919 Avatar asked Mar 28 '13 10:03

user1919


People also ask

How do I post XML data with Curl?

To post XML using Curl, you need to pass XML data to Curl with the -d command line parameter and specify the data type in the body of the POST request message using the -H Content-Type: application/xml command line parameter.

Can Curl send a POST request?

You can send a POST request with Curl by explicitly specifying the POST method with the -X POST command line parameter or passing data to Curl using the -d or --data command line parameter.

How do I post XML to the server?

To post XML data to the server, you need to make an HTTP POST request, include the XML in the body of the request message, and set the correct MIME type for the XML. The correct MIME type for XML is application/xml.

How does Curl respond to XML?

To send XML to the server using Curl, you need to pass the XML data to Curl with the -d command line option and specify the data type in the body of the POST message using the -H "Content-Type: application/xml" command-line option.


1 Answers

Try it this way:

  $url = 'https://android.googleapis.com/gcm/send';
  $ch = curl_init();
  curl_setopt( $ch, CURLOPT_URL, $url );
  curl_setopt( $ch, CURLOPT_POST, true );
  curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $ch, CURLOPT_POSTFIELDS, "<xml>here</xml>" );
  $result = curl_exec($ch);
  curl_close($ch);

For more details visit: http://php.net/manual/en/function.curl-setopt.php

like image 176
PKeidel Avatar answered Sep 19 '22 17:09

PKeidel