Created
August 9, 2013 08:43
-
-
Save tamboer/6192103 to your computer and use it in GitHub Desktop.
post to remote server http://www.bitrepository.com/send-http-post-data-to-remote-url.html Gabriel.C.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //use EXAMPLE | |
| $response = SendPostData($_POST, 'http://www.myserver2location.com/receive-data.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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment