Open-Source PHP Framework - Designed for rapid development of performance-oriented scalable applications

/mvc/components/post

[return to app]
1
<?php
/**
 * Simpifies posting data to a service or other web site
 */
class postComponent {
    /**
     * Sends post request
     *
     * @param string $url
     * @param string $data
     * @param string $header Optional, if ommitted will send the content-type header
     * @return string Response data
     */
    public function postRequest($url, $data, $header = 'Content-Type: application/x-www-form-urlencoded') {
        $params = array('http' => array('method' => 'POST', 'content' => $data, 'header' => $header));
        $ctx = stream_context_create($params);
        $fp = fopen($url, 'rb', false, $ctx);
        if (!$fp) {
            trigger_error('Problem with ' . $url, E_USER_NOTICE);
        }
        $response = stream_get_contents($fp);
        if ($response === false) {
            trigger_error('Problem reading data from ' . $url, E_USER_NOTICE);
        }
        return $response;
    }
}