2

I've seen numerous methods of POSTing data with PHP over the years, but I'm curious what the suggested method is, assuming there is one. Or perhaps there is a somewhat unspoken yet semi-universally-accepted method of doing so. This would include handling the response as well.

Sampson
  • 265,109
  • 74
  • 539
  • 565

5 Answers5

3

While the Snoopy script maybe cool, if you're looking to just post xml data with PHP, why not use cURL? It's easy, has error handling, and is a useful tool already in your bag. Below is an example of how to post XML to a URL with cURL in PHP.

// url you're posting to        
$url = "http://mycoolapi.com/service/";

// your data (post string)
$post_data = "first_var=1&second_var=2&third_var=3";

// create your curl handler     
$ch = curl_init($url);

// set your options     
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //ssl stuff
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:  application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// your return response
$output = curl_exec($ch); 

// close the curl handler
curl_close($ch);
HamZa
  • 14,671
  • 11
  • 54
  • 75
M.W. Felker
  • 4,777
  • 1
  • 20
  • 19
1

cURL is the only reliable way I know of, to POST data, aside from using a socket.

Now if you wanted to send data via GET there are several methods:
cURL
sockets
file_get_contents
file
and others

UnkwnTech
  • 88,102
  • 65
  • 184
  • 229
1

You could try the Snoopy script
It is useful on hosting providers that don't allow fopen wrappers
I have used it for several years to grab RSS feeds.

BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
  • Wow. I just looked at the Snoopy Script and it is brilliantly-simple. I'll definitely check this out. – Sampson Jan 23 '09 at 15:24
  • I think it is even included in some larger open source PHP projects. – BuddyJoe Jan 23 '09 at 17:14
  • The Zend_Http_Client is also a good idea. I would use that if you are going to be using other parts of the Zend Framework. – BuddyJoe Jan 23 '09 at 17:15
  • @Bruno, I am not currently using Zend, nor do I plan to in the future. I do plan on using CodeIgniter though, so I will likely search in that community for potential solutions too. – Sampson Jan 23 '09 at 17:38
  • Then I would lean towards Snoopy, and write a small abstraction or service layer for what you need it for. That way your code relies on your service layer not Snoopy. You'll be able to swap it out later. – BuddyJoe Jan 23 '09 at 21:24
  • Digging up some bones here but unless you need Snoopy for everything it uses, isn't this over kill for this question? brun, not saying that you're wrong here, just making the point that there are other pre-existing solutions without the need to use third-party libraries. – M.W. Felker Feb 01 '12 at 19:30
  • Max, I since have moved away from Snoopy behind a facade to curl behind a facade layer. But might swap curl for Zend_Http_client in the future. – BuddyJoe Feb 02 '12 at 21:27
1

I like Zend_Http_Client from Zend Framework.

It basically works using stream_context_create() and stream_socket_client().

Small example:

$client = new Zend_Http_Client();
$client->setUri('http://example.org');
$client->setParameterPost('foo', 'bar')
$response = $client->request('POST');

$status = $response->getStatus();
$body = $response->getBody();
Karsten
  • 14,572
  • 5
  • 30
  • 35
0

There isn't really a standard way. In code meant for distribution, I generally check cURL, file_get_contents and sockets, using the first one found. Each of those supports GET and POST, and each of those may or may not be available (or work) depending on the PHP version and configuration.

Basically something like:

function do_post($url, $data) {
  if (function_exists('curl_init') && ($curl = curl_init($url))) {
    return do_curl_post($curl, $data);
  } else if (function_exists('file_get_contents') && ini_get('allow_url_fopen') == "1") {
    return do_file_get_contents_post($url, $data);
  } else {
    return do_socket_post($url, $data);
  }
}
waqas
  • 10,323
  • 2
  • 20
  • 11