Send HTTP POST Data to Remote URL
Posted on April 25, 2013, under PHP,
Bookmark it
Hi guys,
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.
/**
* 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.
- April 25, 2013
- article by Gabriel C.
- Leave a reply!
-
April 15, 2013
LayerSlider Responsive WordPress Slider Plugin: 2 Licenses Giveaway
Besides the fact that it looks professional, having a professional slider on top of your page can help you boost your conversion rates by featuring concise content like images, videos on pages where space is limited. It uses eye-catching transition effects to capture the attention of the visitors. Kreatura Media are giving away 2 licenses [...]
-
April 13, 2013
eBook: How to Design the Perfect Web Application – only $16.5!
Designing a successful Web app is a challenging task. You might have the coolest idea ever, yet, if your performance is poor, nobody’s going to use it. Sure, you’ve got web site design down to a science, but developing Web apps is a totally different thing. When you design a Web app, you need to [...]
-
April 12, 2013
The Benefits of Cloud Hosting
Technological advances in the last few years have enabled more and more entrepreneurs to make a living on the Internet through e-commerce sites or blogs. One of the consequences of this has been a massive need for server space. In recent years, cloud hosting has been created as a solution for this. Cloud hosting has [...]
-
March 11, 2013
The Top Reasons That All Bloggers Should Have A Gym Membership
Being successful as a blogger is not just about being good at writing and doing SEO, it’s about creating exactly the right circumstances and exactly the right lifestyle that is conducive to that success. Many people will set themselves blogging goals such as ‘write a post a day’ or ‘spend X amount on SEO’ and [...]
-
March 8, 2013
Motivational and Inspirational Startup (Matte) Posters
There are motivational posters, and then there are successful motivational and inspirational posters. No startup employee would like to look at a boring, cliche saying plastered on the wall at work. They need something trendy, fresh and to the point. Something meaningful, fun and modern. Basically, they gotta have a motivational poster from StartupVitamins! Startups [...]
-
March 3, 2013
Compress PNG Images Online and Make Your Web Pages Load Faster
Some time ago I was writing about a cool tool to compress PNG images through smart lossy compression. I’ve stumbled upon another interesting tool to Compress PNG images into PNG-8 format with transparency support and full browser compatibility. What I like the most about it, is the fact that it gives you flexibility in decreasing [...]
-
March 2, 2013
Tips How To Better Design Your Website
If you are planning on redesigning your website or starting your first web build, you have a bit of planning ahead of you. These days website needs to look professional, with a quality site design, easy-to-use navigation, straightforward functionality and great site contents and copy. Each of these areas is a separate skill set on [...]
-
March 1, 2013
Smashing Bundle: The Essential Coding for Web Design – only $20.4!
One of the neatest parts about programming is the fact that there’s always new stuff to discover. With so much information to soak up you’d literally require a computer chip in your head to remember all of it. For this reason it’s really a smart idea to develop a collection of informational programming books. Regardless [...]
Popular Posts
AJAX Form with CAPTCHA, Realtime Validation and PHP BackendArticle has 552 comments
How to Add Multi-language Support to a PHP WebsiteArticle has 105 comments
Dynamic Dependant DropDown List: US States & CountiesArticle has 87 comments
How to Implement a jQuery AJAX Login Form into a Modal BoxArticle has 85 comments
An AJAX (jQuery) Username Availability Checker with PHP Back-endArticle has 62 comments







