PHP Curl idiomatic functions

Source: Internet
Author: User
Tags http authentication vars
PHP Curl Common functions

PHP curl is commonly used for: Get,post,http authentication,302 redirection, and setting up the agent for Curl.

1. Turn on PHP's Curl function

Under the Windows platform, or using an integration server such as XAMPP, the program will be very simple, you need to change the settings of your php.ini file, find Php_curl.dll, and cancel the previous semicolon comment on the line. As shown below:

Uncomment to turn on the Curl function
Extension=php_curl.dll

Under Linux , then, you need to recompile your PHP, and when you edit it, you need to open the compile parameter--add the "–with-curl" parameter to the Configure command.

2. Use Curl to get data

The simplest and most commonly used PHP function to get the content of a Web page using get
function Getcurl ($url) {
$curl = Curl_init ();
curl_setopt ($curl, Curlopt_url, $url);
curl_setopt ($curl, Curlopt_header, 0);
curl_setopt ($curl, Curlopt_timeout, 3);//Time-out
curl_setopt ($curl, Curlopt_returntransfer, true);
$data = curl_exec ($curl);
Curl_close ($curl);
return $data;
}

3. Post data using Curl

We use the following PHP function When we need to request a POST for a page with Curl request
function _curl_post ($url, $vars) {
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_postfields, $vars);
$data = curl_exec ($ch);
Curl_close ($ch);
if ($data)
return $data;
Else
return false;
}

4, using Curl, requires HTTP server authentication

When we request an address that requires authentication, that is, HTTP server Authentication , we use the following function, which is used in the same way for the get method in curl.
function Postcurlhttp ($url, $str) {
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, CURLOPT_USERPWD, "Authenticated User name: password");
curl_setopt ($ch, Curlopt_postfields, $STR);
$data = curl_exec ($ch);
$Headers = Curl_getinfo ($ch);
if ($Headers [' http_code '] = = 200) {
return $data;
} else {
return false;
}
}

5. Use Curl to get 302 redirected pages

The following function $data the contents of the redirected page, where we write a simple CURL post with 302 redirects returning the function to redirect the page URL, sometimes returning the URL of the page more important.
function _curl_post_302 ($url, $vars) {
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, curlopt_followlocation, 1); 302 redirect
curl_setopt ($ch, Curlopt_postfields, $vars);
$data = curl_exec ($ch);
$Headers = Curl_getinfo ($ch);
Curl_close ($ch);
if ($data && $Headers)
return s$headers["url"];
Else
return false;
}

6. Add a proxy server to curl

$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, 'http://www.js8.in');
curl_setopt ($ch, Curlopt_header, 1);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_httpproxytunnel, 1);
curl_setopt ($ch, Curlopt_proxy, ' Proxy server address (www.js8.in): Port ');
curl_setopt ($ch, curlopt_proxyuserpwd, ' Proxy User: password ');
$data = Curl_exec ();
Curl_close ($ch);

7, a simple class of curl

/*
Sean Huber CURL Library

This library is a basic implementation of CURL capabilities.
It works in the most modern versions of IE and FF.

==================================== USAGE ====================================
It exports the CURL object globally, so set a callback with Setcallback ($func).
(Use Setcallback (the array (' class_name ', ' func_name ')) to set a callback as a Func
That lies within a different class)
Then use one of the CURL request methods:

get ($url);
Post ($url, $vars); VARs is a urlencoded string in query string format.

Your callback function would then is called with 1 argument, the response text.
If a callback is not defined, your request would return the response text.
*/

Class CURL {
var $callback = false;

function Setcallback ($func _name) {
$this->callback = $func _name;
}

function DoRequest ($method, $url, $vars) {
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_header, 1);
curl_setopt ($ch, curlopt_useragent, $_server[' http_user_agent ');
curl_setopt ($ch, curlopt_followlocation, 1);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_cookiejar, ' cookie.txt ');
curl_setopt ($ch, Curlopt_cookiefile, ' cookie.txt ');
if ($method = = ' POST ') {
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_postfields, $vars);
}
$data = curl_exec ($ch);
Curl_close ($ch);
if ($data) {
if ($this->callback)
{
$callback = $this->callback;
$this->callback = false;
Return Call_user_func ($callback, $data);
} else {
return $data;
}
} else {
Return Curl_error ($ch);
}
}

function Get ($url) {
return $this->dorequest (' GET ', $url, ' NULL ');
}

Function post ($url, $vars) {
return $this->dorequest (' POST ', $url, $vars);
}
}
?>

Transferred from :http://blog.phpmake.com/?p=326

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.