PDA

View Full Version : Curl e Libcurl exemplos


suporte
16th May 2008, 15:39
HTTP:

<?
// FIND BOOKS ON PHP AND MYSQL ON AMAZON
$url = "http://www.amazon.com/exec/obidos/search-handle-form/002-5640957-2809605";
$ch = curl_init (http://devzone.zend.com/manual/function.curl-init.php)(); // initialize curl handle
curl_setopt (http://devzone.zend.com/manual/function.curl-setopt.php)($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt (http://devzone.zend.com/manual/function.curl-setopt.php)($ch, CURLOPT_FAILONERROR, 1);
curl_setopt (http://devzone.zend.com/manual/function.curl-setopt.php)($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
curl_setopt (http://devzone.zend.com/manual/function.curl-setopt.php)($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt (http://devzone.zend.com/manual/function.curl-setopt.php)($ch, CURLOPT_TIMEOUT, 3); // times out after 4s
curl_setopt (http://devzone.zend.com/manual/function.curl-setopt.php)($ch, CURLOPT_POST, 1); // set POST method
curl_setopt (http://devzone.zend.com/manual/function.curl-setopt.php)($ch, CURLOPT_POSTFIELDS, "url=index%3Dbooks&field-keywords=PHP+MYSQL"); // add POST fields
$result = curl_exec (http://devzone.zend.com/manual/function.curl-exec.php)($ch); // run the whole process
curl_close (http://devzone.zend.com/manual/function.curl-close.php)($ch);
echo $result;
?>


HTTPS:


<?php
// HTTP authentication
$url = "http://www.example.com/protected/";
$ch = curl_init (http://devzone.zend.com/manual/function.curl-init.php)();
curl_setopt (http://devzone.zend.com/manual/function.curl-setopt.php)($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt (http://devzone.zend.com/manual/function.curl-setopt.php)($ch, CURLOPT_URL, $url);
curl_setopt (http://devzone.zend.com/manual/function.curl-setopt.php)($ch, CURLOPT_USERPWD, "myusername:mypassword");
$result = curl_exec (http://devzone.zend.com/manual/function.curl-exec.php)($ch);
curl_close (http://devzone.zend.com/manual/function.curl-close.php)($ch);
echo $result;
?>


FTP:


<?PHP
// FTP this script to a server
$fp = fopen (http://devzone.zend.com/manual/function.fopen.php)(__FILE__, "r");
$url = "ftp://username:password@mydomain.com:21/path/to/newfile.php";
$ch = curl_init (http://devzone.zend.com/manual/function.curl-init.php)();
curl_setopt (http://devzone.zend.com/manual/function.curl-setopt.php)($ch, CURLOPT_URL, $url);
curl_setopt (http://devzone.zend.com/manual/function.curl-setopt.php)($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt (http://devzone.zend.com/manual/function.curl-setopt.php)($ch, CURLOPT_UPLOAD, 1);
curl_setopt (http://devzone.zend.com/manual/function.curl-setopt.php)($ch, CURLOPT_INFILE, $fp);
curl_setopt (http://devzone.zend.com/manual/function.curl-setopt.php)($ch, CURLOPT_FTPASCII, 1);
curl_setopt (http://devzone.zend.com/manual/function.curl-setopt.php)($ch, CURLOPT_INFILESIZE, filesize (http://devzone.zend.com/manual/function.filesize.php)(__FILE__));
$result = curl_exec (http://devzone.zend.com/manual/function.curl-exec.php)($ch);
curl_close (http://devzone.zend.com/manual/function.curl-close.php)($ch);
?>
Zend Curl Manual: http://devzone.zend.com/manual/ref.curl.php