Post to a Remote Script with cURL
PHP/MySQL
Last Updated: 2007-02-04 08:42:30
Last Updated: 2007-02-04 08:42:30
STEP 1: About cURL
cURL stands for 'Client URL', and is perhaps the most powerful PHP extension available. cURL allows you to communicate with other servers. This may not sound like it's that interesting until you fully understand what you can do with it. For instance you can write internet crawlers that gather information for search engine results (HINT: see my search engine page). You can write merchant account gateways that validate credit card information with your bank in line with your script so there is no refreshing of the page. You can process information on FTP servers. You can gather stock information and weather information from other sites to use on your own. The possibilities are endless.
To use the cURL library you must have cURL installed as an extention to your PHP installation. If you are looking for a web host who already has cURL extentions installed, a good one that I use is fatcow.com. If you do use them be sure to list Joe Fitzgerald as your reference, because they will give me a free month of hosting if you do... and I'd appreciate that.
This tutorial assumes you've been through the first cURL tutorial that you can find here.
To use the cURL library you must have cURL installed as an extention to your PHP installation. If you are looking for a web host who already has cURL extentions installed, a good one that I use is fatcow.com. If you do use them be sure to list Joe Fitzgerald as your reference, because they will give me a free month of hosting if you do... and I'd appreciate that.
This tutorial assumes you've been through the first cURL tutorial that you can find here.
STEP 2: Posting variables
This script POSTS values to a remote script.
<?php
// populate the array with the values to be posted
$postfields = array();
$postfields['field1'] = urlencode('value1');
$postfields['field2'] = urlencode('value2');
// Initialize your cURL session
$ch = curl_init();
// Follow any Location headers
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// tell cURL what file to open
curl_setopt($ch, CURLOPT_URL, 'http://www.amazon.com/Ronin-Frank-Miller/dp/0930289218');
// tell cURL to populate the $ch variable with resutls
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Alert cURL to the fact that we're doing a POST, and pass the associative array for POSTing
curl_setopt($ch, CURLOPT_POST, 1);
// Post your values to the opened script
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
// Execute your cURL script
$output = curl_exec($ch);
// Free system resources taken by cURL
curl_close($ch);
print $output;
?>
CURLOPT_FOLLOWLOCATION ensures any Location headers are followed.
The script above posted our fields as multipart/form-data. You may also send fields as name-value pairs, separated by amphersands. Below is an example of how that syntax would look:
<?php
// Initialize your cURL session
$ch = curl_init();
// Follow any Location headers
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Optionally set a timeout
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// tell cURL what file to open
curl_setopt($ch, CURLOPT_URL, 'http://www.amazon.com/exec/obidos/search-handle-form/002-0565257-5012066');
// tell cURL to populate the $ch variable with resutls
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Alert cURL to the fact that we're doing a POST, and pass the associative array for POSTing
curl_setopt($ch, CURLOPT_POST, 1);
// Post your values to the opened script
curl_setopt($ch, CURLOPT_POSTFIELDS, "url=index%3Dbooks&field-keywords=frank+miller");
// Execute your cURL script
$output = curl_exec($ch);
// Free system resources taken by cURL
curl_close($ch);
print $output;
?>
The above would grab this url:
http://www.amazon.com/exec/obidos/search-handle-form/002-0565257-5012066 ?url=index%3Dbooks&field-keywords=frank+miller
<?php
// populate the array with the values to be posted
$postfields = array();
$postfields['field1'] = urlencode('value1');
$postfields['field2'] = urlencode('value2');
// Initialize your cURL session
$ch = curl_init();
// Follow any Location headers
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// tell cURL what file to open
curl_setopt($ch, CURLOPT_URL, 'http://www.amazon.com/Ronin-Frank-Miller/dp/0930289218');
// tell cURL to populate the $ch variable with resutls
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Alert cURL to the fact that we're doing a POST, and pass the associative array for POSTing
curl_setopt($ch, CURLOPT_POST, 1);
// Post your values to the opened script
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
// Execute your cURL script
$output = curl_exec($ch);
// Free system resources taken by cURL
curl_close($ch);
print $output;
?>
CURLOPT_FOLLOWLOCATION ensures any Location headers are followed.
The script above posted our fields as multipart/form-data. You may also send fields as name-value pairs, separated by amphersands. Below is an example of how that syntax would look:
<?php
// Initialize your cURL session
$ch = curl_init();
// Follow any Location headers
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Optionally set a timeout
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// tell cURL what file to open
curl_setopt($ch, CURLOPT_URL, 'http://www.amazon.com/exec/obidos/search-handle-form/002-0565257-5012066');
// tell cURL to populate the $ch variable with resutls
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Alert cURL to the fact that we're doing a POST, and pass the associative array for POSTing
curl_setopt($ch, CURLOPT_POST, 1);
// Post your values to the opened script
curl_setopt($ch, CURLOPT_POSTFIELDS, "url=index%3Dbooks&field-keywords=frank+miller");
// Execute your cURL script
$output = curl_exec($ch);
// Free system resources taken by cURL
curl_close($ch);
print $output;
?>
The above would grab this url:
http://www.amazon.com/exec/obidos/search-handle-form/002-0565257-5012066 ?url=index%3Dbooks&field-keywords=frank+miller
This is the JoeLucky39 tutorial section. Feel free to use whatever information you find here on your own site(s). Know that not all tutorials are ideal for all sites, so feel free to modify the information contained in this tutorail section as best suits your web site.
At JoeLucky39 you can find tutorials on Comic Art, PHP/MySQL, and more are added all the time. So, visit often and feel free to contact Joe if you think that his tutorials need to be changed or updated.
Joe believes in the open source movement and is happy to share his knowledge with anyone who asks. All Joe asks is, if you use Joe's open source tutorials, you share your knowledge as well.
Thank you and I hope you find what you are looking for. If not feel free to contact me and ask.
