PHP can use curl in LinuxProgramPerform some HTTP interactions, including website logon and post requests. Of course, the system must contain curl, and the second version must be later than 7. For details, refer to the manual.
First, let's look at the simplest way to get an HTTP page:
Code
$ URL = " Http://www.163.com " ; // Take 163 as an Example
$ Session = Curl_init ( $ URL );
Curl_exec ($ Session);
If(Curl_errno ($ Session))//Error Handling
EchoCurl_error ($ Session);
Curl_close ($ Session);
Function is the homepage of get 163. Next, use the curl_setopt function to make some adjustments.
Then let's take a look at the complexity. The function is to log on to the website.
Code
$ URL = " Http://www.abc.com/login.php " ;
$ Postargs = " Name = Jack & Password = 123456 " ;
$ Session = Curl_init ( $ URL );
// Set the postargs parameter and post it.
Curl_setopt ( $ Session , Curlopt_post , True );
Curl_setopt ( $ Session , Curlopt_postfields , $ Postargs );
//Whether to return header information. It can be used to check HTTP return Status values.
Curl_setopt ($ Session,Curlopt_header, True);
// use cookies for logon, note that the corresponding directory permissions are required.
curl_setopt ( $ session , curlopt_cookiefile , " cookie.txt " );
curl_setopt ( $ session , curlopt_cookiejar , " cookie.txt " );
// write the returned result into the variable
curl_setopt ( $ session , curlopt_returntransfer , true );
$ RET = curl_exec ( $ session );
If(Curl_errno ($ Session)){
EchoCurl_error ($ Session);
}
Curl_close ($ Session);
//Output results. After that, you can configure the required items for parse.
Echo $ RET;
It should be noted that the header can be used to check the HTTP status results. For example, the server returns 302 to allow the client to perform jump operations. You can also send header information for authentication.
Curl can also be used for concurrent operations and multi-threaded concurrency, which is better than the pseudo-C .. Example of concurrent processing:
Code
$ Url1 = " Http://www.abc.com/test1.php " ;
$ Url2 = " Http://www.abc.com/test2.php " ;
$ Cl1 =Curl_init ();//Initialize two
$ Cl2 =Curl_init ();
Curl_setopt ($ Cl1,Curlopt_url,$ Url1);
Curl_setopt ($ Cl2,Curlopt_url,$ Url2);
// Continue to use the cookie file. If we have logged on just now and need to maintain the session
Curl_setopt ( $ Cl1 , Curlopt_returntransfer , True );
Curl_setopt ( $ Cl1 , Curlopt_cookiefile , " Cookie.txt " );
Curl_setopt ( $ Cl1 , Curlopt_cookiejar , " Cookie.txt " );
Curl_setopt ( $ Cl2 , Curlopt_returntransfer , True );
Curl_setopt ( $ Cl2 , Curlopt_cookiefile , " Cookie.txt " );
Curl_setopt ( $ Cl2 , Curlopt_cookiejar , " Cookie.txt " );
$ mh = curl_multi_init (); // This is mainly used to initialize multithreading
curl_multi_add_handle ( $ mh , $ cl1 );
curl_multi_add_handle ( $ mh , $ Cl2 );
$ running = null ;
DO {< br> curl_multi_exec ( $ mh , $ running );
} while ( $ running > 0 ); // after running, running is set to 0
// obtain the returned result
$ RET = array ();
$ RET [] = curl_multi_getcontent ( $ cl1 );
$ RET [] = curl_multi_getcontent ( $ Cl2 );
//Close
Curl_multi_remove_handle ($ MH,$ Cl1);
Curl_multi_remove_handle ($ MH,$ Cl2);
Curl_multi_close ($ MH);
Note: The cookie is used here, so that curl can send the cookie content and header while sending the request, so that the session is maintained. In fact, multiple concurrent operations are similar, that is, they are set separately, then added together to the multi function, and then sent together.
Supplement:
The proxy method can be used through
Code
//Set the URL, for example, 127.0.0.1: 1080.
Curl_setopt ($ Session,Curlopt_proxy, $ Proxy_url);
//If necessary, set the account
// Curl_setopt ($ session, curlopt_proxyuserpwd, $ proxy_auth );
Curl_setopt ($ Session,Curlopt_proxytype,Curlproxy_socks5 );
//Or HTTP. HTTP is the default method.
// Curl_setopt ($ session, curlopt_proxytype, curlproxy_http );