PHP CURL Post uses

Source: Internet
Author: User
Tags codeigniter
CURL is a tool that uses URL syntax to transfer files and data, and supports many protocols, such as HTTP, FTP, Telnet, and so on. Best of all, PHP also supports the CURL library. This article describes some of the advanced features of CURL and how to use it in PHP.

Why do you use CURL?

Yes, we can obtain the content of the webpage by other means. Most of the time, I'm just using simple PHP functions because I want to be lazy:

However, this approach lacks flexibility and effective error handling. And you can't use it to accomplish some difficult tasks?? such as processing coockies, verification, form submission, file upload and so on.

Reference:
CURL is a powerful library that supports many different protocols, options, and provides a variety of details about URL requests.

Basic structure

Before learning more complex features, take a look at the basic steps to build a curl request in PHP:

1 initialization

2 Setting variables

3 Execute and get results

4 releasing the Curl handle

The following is the referenced content:

1. Initialization
$ch = Curl_init ();
2. Setting options, including URLs
curl_setopt ($ch, Curlopt_url, "http://www.nettuts.com");
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_header, 0);
3. Execute and get HTML document content
$output = curl_exec ($ch);
4. Releasing the curl handle
Curl_close ($ch);

Check for errors

You can add a section that checks for errors (although this is not required):

$output = curl_exec ($ch);
if ($output = = = FALSE) {
echo "CURL Error:". Curl_error ($ch);
}

Please note that we use "= = = False" instead of "= = false" when comparing. Because we have to distinguish between the null output and the Boolean value false, the latter is the real error.

Send data using the Post method

When a GET request is initiated, the data can be passed to a URL through the query string. For example, when searching in Google, the search key is part of the query string for the URL:

Http://www.google.com/search?q=nettuts

In this case you may not need curl to simulate. Throw this URL to "file_get_contents ()" To get the same result.

However, some HTML forms are submitted using the Post method. When this form is submitted, the data is sent over the HTTP request body, not the query string. For example, when using the CodeIgniter forum form, no matter what keyword you enter, always post to the following page:

http://codeigniter.com/forums/do_search/

You can use PHP scripts to simulate this URL request. First, create a new file that can accept and display the post data

Use discuzX2.0 for testing

Create a cookie Temp file

$cookiefile = Tempnam ('./temp ', ' Cookie ');

$url = ' Http://dx/member.php?mod=logging&action=login&loginsubmit=yes ';

$post _data = Array (

' Loginfield ' = ' username ',

' Username ' = ' ybb ',

' Password ' = ' 123456 ',

);

$ch = Curl_init ();

curl_setopt ($ch, Curlopt_url, $url);

curl_setopt ($ch, Curlopt_header, 0);

curl_setopt ($ch, Curlopt_returntransfer, 1);

We're in post data Oh!

curl_setopt ($ch, Curlopt_post, 1);

Add the post variable to the

curl_setopt ($ch, Curlopt_postfields, $post _data);

Save Cookie File

curl_setopt ($ch, Curlopt_cookiejar, $cookiefile);

$output = curl_exec ($ch);

Debug use

if ($output = = = FALSE) {

echo "CURL Error:". Curl_error ($ch);

}

Curl_close ($ch);

Access to the Posts page

$post _url = ' http://dx/forum.php?mod=post&action=newthread&fid=2 ';

$ch = Curl_init ($post _url);

curl_setopt ($ch, Curlopt_header, 0);

curl_setopt ($ch, Curlopt_returntransfer, 1);

Read Cookie File

curl_setopt ($ch, Curlopt_cookiefile, $cookiefile);

echo $data = curl_exec ($ch);

Curl_close ($ch);

?>

  • 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.