-1
wget -d --header="Content-Type:application/xml" --post-data="$(cat <your xml file>)" http://sample.sample.com/api

How do i use this function in php? I want also to get the response from this function. i have a variable in php the viable is like this

$xml = '<sample>
    <Request target="test">
    </Request>
</sample>'

This is the xml that i want to post.

I tried the following:

$url = 'sample.sample.com/api';;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$(cat <".$xml.">)"); // receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo $server_output = curl_exec ($ch);
curl_close ($ch);

But it returned this error:

Parsing XML failed: Start tag expected, '<' not found

Tchoupi
  • 14,560
  • 5
  • 37
  • 71
  • 2
    Have you tried php curl object ? – georgecj11 Mar 23 '13 at 14:13
  • i dont know how to use curl to function like this wget function ... if you know how to make curl work like this it will be great – Aviv Remona Mar 23 '13 at 14:15
  • 1
    Duplicate of this question: http://stackoverflow.com/questions/3080146/post-data-to-url-php – Jonathan M Mar 23 '13 at 14:16
  • Read http://php.net/manual/en/book.curl.php – Basile Starynkevitch Mar 23 '13 at 14:17
  • There are basic curls examples in the documentation. Read them, try them, and if you run into problems, ask about them here. http://www.php.net/manual/en/curl.examples-basic.php – Tchoupi Mar 23 '13 at 14:17
  • no its not the same i did exaclty like this fucntion i get this response from the server Parsing XML failed: Start tag expected, '<' not found – Aviv Remona Mar 23 '13 at 14:17
  • Then show us what you have tried and we will help. – Tchoupi Mar 23 '13 at 14:18
  • $url = 'http://sample.sample.com/api'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "$(cat <".$xml.">)"); // receive server response ... curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); echo $server_output = curl_exec ($ch); curl_close ($ch); – Aviv Remona Mar 23 '13 at 14:19
  • 2
    @Aviv, please post the code as an edit to your original question. That way it's formatted for easy viewing. – Jonathan M Mar 23 '13 at 14:19

2 Answers2

1

You should be able to follow a similar scenario as shown at Sending and Receiving XML using PHP. The second portion of that site (To send XML) uses curl to handle this operation which has similar properties to wget, but utilizing the PHP library rather than a command line binary and parameters. I'll include this snippet from the site for longevity.

<?php
  /*
   * XML Sender/Client.
   */
  // Get our XML. You can declare it here or even load a file.
  $xml_builder = '
                  <?xml version="1.0" encoding="utf-8"?>
                  <Test>
                      <String>I like Bharath.co.uk!</String>
                  </Test>
                 ';
  // We send XML via CURL using POST with a http header of text/xml.
  $ch = curl_init('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
  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_builder);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  curl_setopt($ch, CURLOPT_REFERER, 'http://www.bharath..co.uk');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $ch_result = curl_exec($ch);
  curl_close($ch);
  // Print CURL result.
  echo $ch_result;
?>
Mark Stanislav
  • 979
  • 4
  • 11
  • if i use this curl_init('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']); and run the script it just never finishes loading, but if i change it to $ch = curl_init(); it runs but i get nothing in response – Aviv Remona Mar 23 '13 at 14:37
  • With curl_init, it will set CURLOPT_URL to the value passed to the function. So for you try, http://sample.sample.com/api – Mark Stanislav Mar 23 '13 at 14:40
0

I think so ...

$cmd = "wget -d --header=\"Content-Type:application/xml\" --post-data=\"$(cat <your xml file>)\" http://sample.sample.com/api";
exec($cmd);
$outputfile = "dl.html";
echo file_get_contents($outputfile);
Peter
  • 34
  • 5