17

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

user1919
  • 3,818
  • 17
  • 62
  • 97

2 Answers2

34

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

PKeidel
  • 2,559
  • 1
  • 21
  • 29
  • So inside the tags I put all the xml string? – user1919 Mar 28 '13 at 10:29
  • 1
    If your whole xml is in $xml that line should be: curl_setopt( $ch, CURLOPT_POSTFIELDS, $xml ); – PKeidel Mar 28 '13 at 10:30
  • Another thing I haven't understood clearly is what the $url variable contains. Is the hostname of the server which I will send the request? – user1919 Mar 28 '13 at 10:32
  • Thats the same URL like when you browse the receiving script via a browser. When you want to send the POST to the PHP script "run.php" on the server example.com within a subfolder called "api" your $url must be "http://example.com/api/run.php" – PKeidel Mar 28 '13 at 10:40
  • So in my case writing this would be enough: curl_setopt($ch, CURLOPT_URL, 'sendRequest.php'); Where the sendRequest.php contains all the above code. Right? – user1919 Mar 28 '13 at 10:45
  • No, with CURLOPT_URL you specify the url where the data are sended to. You said you want so send a POST request to a web service. So $url is the full url of this web service. – PKeidel Mar 28 '13 at 11:24
  • Aha! I see! Thanks for the help. I will try a bit later to make it work! – user1919 Mar 28 '13 at 11:43
  • 2
    No problem. You have asked 5 questions already, you should start accepting some answers ;) – PKeidel Mar 28 '13 at 12:10
-1

I think using the HTTP classes may be better suited for making HTTP requests.

See http://www.php.net/manual/intro.http.php .

Also, there are specific WMS libraries for PHP, e.g. http://docs.huihoo.com/geoserver/1.6.0/Parsing%20and%20using%20WMS%20capabilities%20with%20PHP.html .

til_b
  • 327
  • 5
  • 15