/mvc/components/post
[return to app]1
<?php
2 /**
3 * Simpifies posting data to a service or other web site
4 */
5 class postComponent {
6 /**
7 * Sends post request
8 *
9 * @param string $url
10 * @param string $data
11 * @param string $header Optional, if ommitted will send the content-type header
12 * @return string Response data
13 */
14 public function postRequest($url, $data, $header = 'Content-Type: application/x-www-form-urlencoded') {
15 $params = array('http' => array('method' => 'POST', 'content' => $data, 'header' => $header));
16 $ctx = stream_context_create($params);
17 $fp = fopen($url, 'rb', false, $ctx);
18 if (!$fp) {
19 trigger_error('Problem with ' . $url, E_USER_NOTICE);
20 }
21 $response = stream_get_contents($fp);
22 if ($response === false) {
23 trigger_error('Problem reading data from ' . $url, E_USER_NOTICE);
24 }
25 return $response;
26 }
27 }