cURL cURL > libcurl > PHP > Examples

PHP/CURL Examples Collection

We try to collect examples on how to program the PHP/CURL interface here. If you have any source snippests you want to share with the rest of the world, please let us know!

ExampleDescriptionAuthor
blogpost Post a blog entry to blogger.com using their "Atom" XML-based API. Michael Phipps
callbacks Set callback functions to receive the HTTP response as it comes through. Keyvan Minoukadeh
cookiejar Login to on one page and then get another page passing all cookies from the first page along Mitchell
customrequest CURLOPT_CUSTOMREQUEST usage shown
ebay_login Mimics a browser and logs in to ebay Imran Khalid
ftpupload FTP upload to a remote site Daniel Stenberg
getbinarypageinvar Get the contents of a web page as a binary into a php variable.
getpageinvar Get the contents of a remote web page into a PHP variable
google-analytics-login This code posts all the correct post fields to Googles universal Account Services login and brings the user directly to the AdSense Overview page. Cookies are used in this example as well as setting the USER AGENT and REFERRER HTTP headers. AskApache.com
httpfileupload Shows how to submit and upload a file to a HTML upload form. Daniel Stenberg
linkchecker Check for a given link in a remote page Michael Phipps
multi Shows how to fetch several documents at once by using the multi interface Roman Rozanov
multipartpost Using a multipart/form-post file upload form on a web page. Pete James
put Perform a HTTP PUT to a remote site Julian Bond
resizejpg Download a remote image, resize it and show it to the user Michael Phipps
rss-adsense Follow your Adsense earnings with an RSS reader Ozh
simpleget Get a URL
simplepost Basic HTTP POST operation

The ftpupload.php Example

FTP upload to a remote site Written by Daniel Stenberg

<?php
//
// A simple PHP/CURL FTP upload to a remote site
//

$localfile = "me-and-my-dog.jpg";
$ftpserver = "ftp.mysite.com";
$ftppath   = "/path/to";
$ftpuser   = "myname";
$ftppass   = "mypass";

$remoteurl = "ftp://${ftpuser}:${ftppass}@${ftpserver}${ftppath}/${localfile}";

$ch = curl_init();

$fp = fopen($localfile, "rb");

// we upload a JPEG image
curl_setopt($ch, CURLOPT_URL, $remoteurl);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);

// set size of the image, which isn't _mandatory_ but helps libcurl to do
// extra error checking on the upload.
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));

$error = curl_exec($ch);

// check $error here to see if it did fine or not!

curl_close($ch); 
?>