cURL (Client URL Library Functions)
- Translated as the description of the author when Daniel Stenberg
curl is a comand line tool for transferring files with URL syntax from the command line using URL syntax file transfer program - I want the address of the page that I put an arbitrary value of the values and return values from page retrieving plays a role.
- Most people want to use cURL in PHP probably be due to an HTTPS connection.
socket or other connection method, which many bother to use cURL thing in terms of speed even faster and easier to access because HTTPS. - cURL module must be installed on the server (Linux - curl.so, Windows - php_curl.dll expansion module required)
Available as a cURL
- HTTPS certificates
- HTTP POST
- HTTP PUT
- FTP upload
- HTTP Form
- cookie
- authentication
cURL, Client URL Library Functions
- curl_init : session initiation, return haendeulgap
- curl_setopt : option settings
- curl_exec : Run the curl
- curl_errno : retrieve the error number.
- curl_error : produces an error message.
- curl_getinfo : Status information is returned.
- curl_close : Close the curl session
[Example 1: POST data in a way (simple)]
$ post_data = array (
"name" => "John Doe",
"Birthday" => "1980-08-20"
);
$ ch = curl_init ();
curl_setopt ($ ch, CURLOPT_URL, http://www. example.com ); curl_setopt ($ ch, CURLOPT_POST, an); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data); curl_exec ($ ch); ?>
[Example 2: POST data in a way (function)]
function fetch_page ($ url, $ param, $ cookies, $ referer_url) {
if (strlen (Trim ($ referer_url)) == 0) $ referer_url = $ url;
$ curlsession = curl_init ();
curl_setopt ($ curlsession, CURLOPT_URL, \ "$ url \");
curl_setopt ($ curlsession, CURLOPT_POST, an);
curl_setopt ($ curlsession, CURLOPT_POSTFIELDS, \ "$ param \");
curl_setopt ($ curlsession, CURLOPT_POSTFIELDSIZE, 0);
curl_setopt ($ curlsession, CURLOPT_TIMEOUT, sixty);
if ($ && $ cookies cookies! = \ "\") {
curl_setopt ($ curlsession, CURLOPT_COOKIE, \ "$ cookies \");
}
curl_setopt ($ curlsession, CURLOPT_HEADER, one); / / hedeogap is used to import. To get a cookie calm.
curl_setopt ($ curlsession, CURLOPT_USERAGENT, \ "Mozilla/4.0 (Compatible; MSIE 5.01; Windows NT 5.0) \");
curl_setopt ($ curlsession, CURLOPT_REFERER, \ "$ referer_url \");
ob_start () ;
$ res = curl_exec ($ curlsession);
$ buffer = ob_get_contents ();
ob_end_clean ();
if (! $ buffer) {
$ returnVal = \ "Curl Fetch Error: \". curl_error ($ curlsession);
} else {
$ returnVal = $ buffer;
}
curl_close ($ curlsession);
Return $ returnVal;
}
?>
[Example 3: Transfer Files
$ post_data ['Data [0]'] = "@ image/img_01.jpg";
$ post_data ['Data [0]'] = "@ image/img_02.jpg";
$ ch = curl_init ();
curl_setopt ($ ch, CURLOPT_URL, http://www.example.com/upload.php ); curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data); $ postResult = curl_exec ($ ch) ; ?>
$ ch = curl_init ();
curl_setopt ($ ch, CURLOPT_URL, " https://www.test.com "); / / access the URL address
curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE); / / Check the certificate looks like true dll, a lot of time.
/ / default value is true, be careful, because this part (https connection required at the time)
curl_setopt ($ ch, CURLOPT_SSLVERSION, three); / / SSL version (https connection required at the time)
curl_setopt ($ ch , CURLOPT_HEADER, 0); / / the header output or
curl_setopt ($ ch, CURLOPT_POST, 1); / / Post Get Access or
curl_setopt ($ ch, CURLOPT_POSTFIELDS, "var1 = str1 & var2 = str2"); / / Post Value Get like the way Write.
curl_setopt ($ ch, CURLOPT_TIMEOUT, 30); / / TimeOut value
curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); / / Get result is
$ result = curl_exec ($ ch);
curl_close ($ ch);
echo $ result;
?>
[Example 5: curl using the Gmail login]
$ Src = " https:// ". $ gmailId.": ". $ gmailPw." @ mail.google.com / mail / feed / atom ";
$ ch = curl_init ();
curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, TRUE );
curl_setopt ($ ch, CURLOPT_TIMEOUT, 10);
curl_setopt ($ ch, CURLOPT_HEADER, 0);
curl_setopt ($ ch, CURLOPT_POST, true);
curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ ch, CURLOPT_URL, $ url);
curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt ($ ch, CURLOPT_USERAGENT, 'My Agent Name');
curl_setopt ($ ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ ch, CURLOPT_COOKIEFILE, 'cookie . txt ');
$ res = curl_exec ($ ch);
curl_close ($ ch);
/ ** The result is an Atom xml format. Using DOM or xml parsing function is to parse. ** /
echo $ res;
?>
$ ch = curl_init ();
curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, TRUE );
curl_setopt ($ ch, CURLOPT_TIMEOUT, 10);
curl_setopt ($ ch, CURLOPT_HEADER, 0);
curl_setopt ($ ch, CURLOPT_POST, true);
curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ ch, CURLOPT_URL, $ url);
curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt ($ ch, CURLOPT_USERAGENT, 'My Agent Name');
curl_setopt ($ ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ ch, CURLOPT_COOKIEFILE, 'cookie . txt ');
$ res = curl_exec ($ ch);
curl_close ($ ch);
/ ** The result is an Atom xml format. Using DOM or xml parsing function is to parse. ** /
echo $ res;
?>
[Example 6: cURL web page using the Import
function get_content ($ url) {
$ Agent = 'Mozilla/4.0 (Compatible; MSIE 6.0; Windows NT 5.0)';
$ curlsession = curl_init ();
curl_setopt ($ curlsession, CURLOPT_URL, $ url);
curl_setopt ($ curlsession, CURLOPT_HEADER, 0);
curl_setopt ($ curlsession, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ curlsession, CURLOPT_POST, 0);
curl_setopt ($ curlsession, CURLOPT_USERAGENT, $ Agent);
curl_setopt ($ curlsession, CURLOPT_REFERER, "");
curl_setopt ($ curlsession, CURLOPT_TIMEOUT, 3);
$ buffer = curl_exec ($ curlsession);
$ Cinfo = curl_getinfo ($ curlsession);
curl_close ($ curlsession);
if ($ Cinfo ['http_code']! = two hundred)
{
Return "" ;
}
Return $ buffer;
}
?>