Php practice 15th days _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php practices: 15th days. Today, I learned again that curlPHP provides curl _ * series of functions to operate curl. The following are commonly used: Curl_init initializes a curl session Curl_close. of course, this is disabled. today, I will study curl again.


PHP provides curl _ * series of functions to operate curl.

The following are commonly used:
 Curl_init initializes a curl session
 Curl_close: of course, this is disabled --!
 Curl_error: the error message of the current session is returned.
Invalid Curl_errno error number
Curl_setopt is very important to set an option.
Curl_setopt_array is the same as the above curl_setopt. The difference is that multiple options can be set at this time.
 Curl_exec executes the curl session

These algorithms are commonly used. if you use other functions, you must Google them.

Next we will simulate the GET request:

[Php]
$ Ch = curl_init (); // A session is initialized here.
Curl_setopt ($ ch, CURLOPT_URL, 'http: // www.google.com '); // sets an address related to the $ ch session.
Curl_exec ($ ch); // executes the session
Curl_close ($ ch); // close the session

$ Ch = curl_init (); // A session is initialized here.
Curl_setopt ($ ch, CURLOPT_URL, 'http: // www.google.com '); // sets an address related to the $ ch session.
Curl_exec ($ ch); // executes the session
Curl_close ($ ch); // close the session
After browsing, google's homepage content is output in the browser.
If you ask, what if I don't want his output, but return?
Add an option.


[Php]
$ Ch = curl_init (); // A session is initialized here.
Curl_setopt ($ ch, CURLOPT_URL, 'http: // www.google.com '); // sets an address related to the $ ch session.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // tell curl here that I don't want to output what I want to return
$ Data = curl_exec ($ ch); // The curl will be returned to you during execution. Haha, so obedient ..
Curl_close ($ ch); // close the session

$ Ch = curl_init (); // A session is initialized here.
Curl_setopt ($ ch, CURLOPT_URL, 'http: // www.google.com '); // sets an address related to the $ ch session.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // tell curl here that I don't want to output what I want to return
$ Data = curl_exec ($ ch); // The curl will be returned to you during execution. Haha, so obedient ..
Curl_close ($ ch); // when the session is closed, the GET request is simulated, and the POST is simulated as follows:
[Php] view plaincopyprint? // Here I used my own program to do the experiment ..--!
$ Url = 'http: // www.phpfamily.cn/Shop/login? Formaction = login '; // address to which the POST is sent
$ Query = 'name = xiaokai & password = xiaokai '; // the submitted data.
$ Ch = curl_init ($ url); // A url address is associated during initialization.
Curl_setopt ($ ch, CURLOPT_POST, true); // tell him here that I want to use the post method
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ query); // post the data to him.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // do not output to return
$ Data = curl_exec ($ ch); // OK. it is returned to you after execution.
Curl_close ($ ch); // Close
Echo $ data; // output result, prompting that the logon is successful.

// Here I used my own program to do the experiment ..--!
$ Url = 'http: // www.phpfamily.cn/Shop/login? Formaction = login '; // address to which the POST is sent
$ Query = 'name = xiaokai & password = xiaokai '; // the submitted data.
$ Ch = curl_init ($ url); // A url address is associated during initialization.
Curl_setopt ($ ch, CURLOPT_POST, true); // tell him here that I want to use the post method
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ query); // post the data to him.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // do not output to return
$ Data = curl_exec ($ ch); // OK. it is returned to you after execution.
Curl_close ($ ch); // Close
Echo $ data; // output result, prompting that the logon is successful.
Do not copy the code by yourself, because you will never learn to copy the code.
After the above code is executed, a message is displayed, indicating that the logon is successful.
However, there is a problem, so that the login fails if the refresh login is not saved, and the problem is that the cookie is not enabled in your browser.
Same. Next we will open a cookie for curl.

[Php]
$ Url = 'http: // www.phpfamily.cn/Shop/login? Formaction = login '; // address to which the POST is sent
$ Query = 'name = xiaokai & password = xiaokai '; // the submitted data.
$ Jar = realpath('cookie.txt '); // address of cookie storage
$ Ch = curl_init ($ url); // A url address is associated during initialization.
Curl_setopt ($ ch, CURLOPT_POST, true); // tell him here that I want to use the post method
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ query); // post the data to him.
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ jar); // The address for saving the cookie file. then, the cookie
The content is written to the cookie file.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // do not output to return
$ Data = curl_exec ($ ch); // OK. after the command is executed, it is returned to you.
Curl_close ($ ch); // Close
Echo $ data; // output result, prompting that the logon is successful.

$ Url = 'http: // www.phpfamily.cn/Shop/login? Formaction = login '; // address to which the POST is sent
$ Query = 'name = xiaokai & password = xiaokai '; // the submitted data.
$ Jar = realpath('cookie.txt '); // address of cookie storage
$ Ch = curl_init ($ url); // A url address is associated during initialization.
Curl_setopt ($ ch, CURLOPT_POST, true); // tell him here that I want to use the post method
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ query); // post the data to him.
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ jar); // The address for saving the cookie file. then, the cookie
The content is written to the cookie file.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // do not output to return
$ Data = curl_exec ($ ch); // OK. after the command is executed, it is returned to you.
Curl_close ($ ch); // Close
Echo $ data; // output result, prompting that the logon is successful.
Now, you have added the CURLOPT_COOKIEJAR option and OK. Is it easy. Note that
The value of CURLOPT_COOKIEJAR must be an absolute path, that is, the cookie file storage path you specify must be an absolute path.


[Php]
$ Url = 'http: // www.phpfamily.cn/shop/register ';
$ Jar = realpath('cookie.txt '); // address of cookie storage
$ Ch = curl_init ($ url );
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ jar); // specify the path to save the cookie file.
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 '); // address of cookie storage
$ Ch = curl_init ($ url );
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ jar); // specify the path to save the cookie file.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
$ Data = curl_exec ($ ch );
Curl_close ($ ch); // Close
Echo $ data;
Continue. if the cookie is saved above, you do not need to POST it when simulating logon again. add an option.
CURLOPT_COOKIEFILE can be directly logged on.
Preview again and you will be prompted that you have logged on. In this way, the cookie is saved after login, and then everything can be done, such as submitting
Comments, messages, and so on.

Curl PHP provides curl _ * series functions to operate curl. The following are commonly used: Curl_init initializes a curl session Curl_close, which of course is disabled...

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.