Phpcurlget, post, and cookie are similar to host service providers such as dreamhost, and are used to display fopen. Php curl supports FTP, FTPS, http htpps scp sftp tftp telnet dict file and LDAP. Curl supports SSL certificates, http post, http put, and FTP uploads, kerberos, HTT-based Upload, proxy, cookie, user + password proof, file transfer recovery, and http proxy channel are the most common methods based on http get and post.
1. http get implementation
$ Ch = curl_init ("http://www.domain.com/api/index.php? Test = 1 ");
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // get data and return
Curl_setopt ($ ch, CURLOPT_BINARYTRANSFER, true); // when CURLOPT_RETURNTRANSFER is enabled, data is returned.
Echo $ output = curl_exec ($ ch );
/* Write file */
$ Fh = fopen ("out.html", 'w ');
Fwrite ($ fh, $ output );
Fclose ($ fh );
2. http post implementation
$ Url = 'http: // www.domain.com/api /';
$ Fields = array (
'Lname' => 'justencoding ',
'Fname' => 'phplover ',
'Title' => 'myapi ',
'Age' => '27 ',
'Email '=> '2017 @ gmail.com ',
'Phone' => '123'
);
// $ Post_data = implode ('&', $ fields );
// Open connection
$ Ch = curl_init ();
// Set the url, number of POST vars, POST data
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_POST, count ($ fields); // When enabled, a conventional POST request is sent. the type is application/x-www-form-urlencoded, just like submitting a form.
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields); // The "POST" operation in HTTP. If you want to transfer a file, you need a file name starting @.
Ob_start ();
Curl_exec ($ ch );
$ Result = ob_get_contents ();
Ob_end_clean ();
Echo $ result;
// Close connection
Curl_close ($ ch );
If ($ _ GET ['test'])
{
Print_r ($ _ GET );
}
If ($ _ POST)
{
Print_r ($ _ POST );
}
3. php curl transfer cookie
Two methods:
One is automatic:
Curl_setopt ($ curlHandle, CURLOPT_COOKIEJAR, 'cookie.txt '); // Save
Curl_setopt ($ curlHandle, CURLOPT_COOKIEFILE, 'cookie.txt '); // Read
In this way, the COOKIE will be automatically followed up.
However, it takes two times. one is to generate a cookie first, and then connect it to use the cookie.
Example:
Function get_curlcuconent2 ($ filename, $ referer)
{
$ Cookie_jar = tempnam ('./tmp', 'jsessionid ');
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ filename );
Curl_setopt ($ ch, CURLOPT_HEADER, false );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
// Set the cookie path for file reading and submission
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_jar );
$ Filecontent = curl_exec ($ ch );
Curl_close ($ ch );
$ Ch = curl_init ();
$ Hostname = "www.domain.com ";
// $ Referer = "http://www.domain.com /";
Curl_setopt ($ ch, CURLOPT_URL, $ filename );
Curl_setopt ($ ch, CURLOPT_REFERER, $ referer); // You can also say that you are from google
Curl_setopt ($ ch, CURLOPT_USERAGENT, "www.domain.com ");
// $ Request = "JSESSIONID = abc6szw15ozvZ_PU9b-8r"; // set the POST parameter
// Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ request );
// The above sentence, of course, you can say that you are a baidu. if you get rid of the value here, it will be OK. you can implement the thief function, $ _ SERVER ['http _ USER_AGENT ']
// You can also create a spider by yourself, so pretend to be the CURLOPT_USERAGENT here.
// If you want to put this program on linux and execute it using php-q, you should also write the specific $ _ SERVER ['http _ USER_AGENT ']. forged programs can also be used.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie_jar );
Curl_setopt ($ ch, CURLOPT_HEADER, false); // you can specify whether to output the page content.
Curl_setopt ($ ch, CURLOPT_GET, 1); // post, get past
$ Filecontent = curl_exec ($ ch );
Preg_match_all ("/charset = (. + ?) [NULL \ "\ ']/is", $ filecontent, $ charsetarray );
If (strtolower ($ charsetarray [1] [0]) = "UTF-8 ")
$ Filecontent = iconv ('utf-8', 'gb18030 // IGNORE ', $ filecontent );
Curl_close ($ ch );
Return $ filecontent;
}
?>
Custom:
$ Header [] = 'accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, text/html, *'. '/*';
$ Header [] = 'Accept-Language: zh-cn ';
$ Header [] = 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;. net clr 2.0.50727 )';
$ Header [] = 'host: '. $ your target Host;
$ Header [] = 'connection: Keep-Alive ';
$ Header [] = 'cookie: '. $ your Cookie string;
Curl_setopt ($ curlHandel, CURLOPT_HTTPHEADER, $ header );