PHP Combat 15th Day _php Tutorial

Source: Internet
Author: User
Learn Curl again today


PHP provides a series of curl_* functions to manipulate curl.

Commonly used are the following:
curl_init initialization of a Curl session
curl_close This of course is closed--!
curl_error returns the error message for the current session
curl_errno Error number
curl_setopt setting an option, this function is important
curl_setopt_array this is the same as the above curl_setopt, the difference is that this time you can set multiple options
curl_exec performing a Curl session

These are more commonly used, and other functions if you want to use Google.

Let's simulate a GET request:

[PHP]
$ch = Curl_init ();//Initialization of a session here
curl_setopt ($ch, Curlopt_url, ' http://www.google.com ');//set an address associated with a $ch session
Curl_exec ($ch);//execute session
Curl_close ($ch);//Close session

$ch = Curl_init ();//Initialization of a session here
curl_setopt ($ch, Curlopt_url, ' http://www.google.com ');//set an address associated with a $ch session
Curl_exec ($ch);//execute session
Curl_close ($ch);//Close session
Browsing, Google's home page content is output in the browser.
If you ask that I do not want him to output, but return it?
Then add an option to the line.


[php]
$ch = Curl_init ();//Initialize a session here
Curl_setopt ($ch, Curlopt_url, ' http://www.google.com ');//Set A CH session-related address
curl_setopt ($ch, Curlopt_returntransfer, true);//Tell curl here I do not output I want to return
$data = curl_exec ($ch);// Then curl will return to you at the time of execution. Haha, too obedient.
Curl_close ($ch);//Close session

$ch = Curl_init ();//Initialize a session here
Curl_setopt ($ch, Curlopt_url, ' http://www.google.com ');//set an address associated with $ch session
curl_setopt ($ch, Curlopt_returntransfer, true);//Tell curl here I do not output I want to return
$data = curl_exec ($ch);//Then Curl will return to you at the time of execution. Haha, too obedient.
Curl_close ($ch);//Close the session above is a mock get request, then the following to simulate the post:
[PHP] View plaincopyprint?//Here I use my own program to do the experiment.
$url = ' http://www.phpfamily.cn/Shop/login?formaction=login ';//post to address
$query = ' Name=xiaokai&password =xiaokai ';//This is the data submitted
$ch = Curl_init ($url);//When initializing, associate a URL address
curl_setopt ($ch, Curlopt_post, true);// Here, tell him I'm going to post.
curl_setopt ($ch, Curlopt_postfields, $query);//Send him a post data
curl_setopt ($ch, Curlopt_ Returntransfer, True);//I do not output to return
$data = curl_exec ($ch);//ok, I return it to you after execution.
Curl_close ($ch);//close
echo $data;//output result, prompt login succeeded.

Here's my own program to do the experiment. - -!
$url = ' http://www.phpfamily.cn/Shop/login?formaction=login ';//post to address
$query = ' Name=xiaokai&password=xiaokai ';//This is the submitted data
$ch = Curl_init ($url);//When initializing, associate a URL address
curl_setopt ($ch, Curlopt_post, true);//Here tell him I'm going to POST
curl_setopt ($ch, Curlopt_postfields, $query);//Post data for him
curl_setopt ($ch, Curlopt_returntransfer, true);//I do not output to return
$data = curl_exec ($ch);//ok, return to you after execution.
Curl_close ($ch);//Close
echo $data;//output result, prompt login succeeded.
Write your own code, not copy, because copy you will never learn.
The above code will prompt the login success after execution, and the login is successful.
However, there is a problem, so that login does not save the update login is invalid, this problem and your browser does not open cookies are
The same. So let's start by giving Curl a cookie.

[php]
$url = ' http://www.phpfamily.cn/Shop/login?formaction=login ';//post to address
$query = ' name=xiaokai& Password=xiaokai ';//This is the data submitted
$jar = Realpath (' cookie.txt ');//cookie saved address
$ch = Curl_init ($url);// The initialization is associated with a URL address
curl_setopt ($ch, Curlopt_post, true);//This tells him I'm going to use the POST method
curl_setopt ($ch, Curlopt_postfields, $query);//give him the post data
curl_setopt ($ch, Curlopt_cookiejar, $jar);//give him the address of the cookie file, and the cookie will be automatically
The contents are written to the cookie file.
curl_setopt ($ch, Curlopt_returntransfer, true);//I do not output to return
$data = curl_exec ($ch);//ok, return to you after execution
Curl_ Close ($ch);//Closes the
echo $data;//outputs The result, indicating that the login was successful.

$url = ' http://www.phpfamily.cn/Shop/login?formaction=login ';//post to address
$query = ' name=xiaokai&password= Xiaokai ';//This is the data submitted
$jar = Realpath (' Cookie.txt '),//cookie saved address
$ch = Curl_init ($url);//The URL is associated with the address
when initializing curl_setopt ($ch, Curlopt_post, true);//This tells him I'm going to post the
curl_setopt ($ch, Curlopt_postfields, $query);//give him post data
curl_setopt ($ch, Curlopt_cookiejar, $jar);//This gives him the address of the cookie file, and then automatically writes the cookie
to the cookie file.
curl_setopt ($ch, Curlopt_returntransfer, true);//I do not output to return
$data = curl_exec ($ch);//ok, return to you after execution
Curl_ Close ($ch);//Closes the
echo $data;//outputs The result, indicating that the login was successful.
OK, just add a curlopt_cookiejar option is OK, is not very simple. Note that the value of
Curlopt_cookiejar here must be an absolute path, that is, the cookie file you specify must be the absolute path to save the path.


[PHP]
$url = ' Http://www.phpfamily.cn/Shop/register ';
$jar = Realpath (' cookie.txt ');//cookie saved Address
$ch = Curl_init ($url);
curl_setopt ($ch, Curlopt_cookiefile, $jar);//Specify the path where the cookie file is saved
curl_setopt ($ch, Curlopt_returntransfer, true);
$data = curl_exec ($ch);
Curl_close ($ch);//Close
Echo $data;

$url = ' Http://www.phpfamily.cn/Shop/register ';
$jar = Realpath (' cookie.txt ');//cookie saved Address
$ch = Curl_init ($url);
curl_setopt ($ch, Curlopt_cookiefile, $jar);//Specify the path where the cookie file is saved
curl_setopt ($ch, Curlopt_returntransfer, true);
$data = curl_exec ($ch);
Curl_close ($ch);//Close
Echo $data;
Go ahead, save the cookie on it. Then you don't have to post when you impersonate the login again, add an option
Curlopt_cookiefile will be able to log in directly.
Preview again and you'll be prompted to sign in. When you log in, you save the cookie and then you can do anything, such as submitting
Comments, messages, and so on.

http://www.bkjia.com/PHPjc/477350.html www.bkjia.com true http://www.bkjia.com/PHPjc/477350.html techarticle Learn again Today Curl PHP provides a series of curl_* functions to operate on curl. Commonly used are the following: Curl_init Initializes a Curl session Curl_close this of course is off ...

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