Monday, March 16

Send HTTP POST Data to Remote URL.


    I’d like to share with you a function that I use a lot in my web projects. It’s useful to send data through POST from one location to another. It’s very useful if you need to send information from a server to a remote one. For example, you can use AJAX to call a PHP file from your website that when being accessed, it will send POST data to a Remote URL location and get its output, all of this being processed in the background.

    <?php
    /**
     * SendPostData()
     *
     * @param mixed $_p
     * @param mixed $remote_url
     * @return
     */
    function SendPostData($_p, $remote_url) {
        $remote_url = trim($remote_url);
    
        $is_https = (substr($remote_url, 0, 5) == 'https');
    
        $fields_string = http_build_query($_p);
    
        // Run this code if you have cURL enabled
        if(function_exists('curl_init')) {
            
            // create a new cURL resource
            $ch = curl_init();
            
            // set URL and other appropriate options
            curl_setopt($ch, CURLOPT_URL, $remote_url);
            
            if($is_https && extension_loaded('openssl')) {
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            }
            
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);          
            curl_setopt($ch, CURLOPT_HEADER, false);
    
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            
            // grab URL and pass it to the browser
            $response = curl_exec($ch);
                        
            // close cURL resource, and free up system resources
            curl_close($ch);
    
        // No cURL? Use an alternative code 
        } else {
            
            $context_options = array (
                'http' => array (
                    'method' => 'POST',
                    'header' => "Content-type: application/x-www-form-urlencoded\r\n".
                                "Content-Length: ".strlen($fields_string)."\r\n",
                    'content' => $fields_string
                 )
             );
    
            $context = stream_context_create($context_options);
            $fp = fopen($remote_url, 'r', false, $context);
    
            if (!$fp) {
                throw new Exception("Problem with $remote_url, $php_errormsg");
            }
    
            $response = @stream_get_contents($fp);
    
            if ($response === false) {
                throw new Exception("Problem reading data from $remote_url, $php_errormsg");
            }
        }
        return $response;
    }
    ?>


Usage Example:

    $response = SendPostData($_POST, 'http://www.myserver2location.com/receive-data.php');

PS: You can use any array variable as the first argument. $_POST is just a common one used in these situations.

SOURCE: www.bitrepository.com

No comments:

Post a Comment