PHP Curl Cookie Access sample (Standard acquisition program) and camouflage

Source: Internet
Author: User

PHP Curl Cookie Access sample

A lot of people sent messages asking curl to access cookie files, Du Dong didn't think it was a difficult task, because it was easy to see the manual. Here is an example, after reading it all clear:

<?php
$cookie _jar_index = Dir (__file__). " /".' Cookie.txt ';
 
$url = "http://www.71j.cn/perl/login.pl";
$params = "username=dudu&password=****";
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_cookiejar, $cookie _jar_index);
curl_setopt ($ch, Curlopt_cookie, "fruit=apple; Colour=red ");
The code above is a direct pass of cookie information rather than file
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_postfields, $params); 
curl_setopt ($ch, curlopt_nobody, 1);//This cannot be opened, otherwise the cookie file cannot be generated
ob_start ();
Curl_exec ($ch);
Curl_close ($ch);
Ob_clean ();
 
$url = "http://www.71j.cn/perl/myfavorites.pl";
$ch 2 = Curl_init ();
curl_setopt ($ch 2, Curlopt_url, $url);
curl_setopt ($ch 2, Curlopt_cookiefile, $cookie _jar_index);
Ob_start ();
Curl_exec ($ch 2);
Curl_close ($ch 2);
$rs = Ob_get_contents (); $rs is the returned content
Ob_clean ();
 
Print_r ($RS); 
? >

CURL IP camouflage, changing IP header information

<?php
$ip = rand (1,255). ".". Rand (1,255). ".". Rand (1,255). ".". Rand (1,255). "";
$ip = ' 8.8.8.8 ';
$ch = Curl_init ();
curl_setopt ($ch, curlopt_returntransfer,1);
curl_setopt ($ch, curlopt_http_version,curl_http_version_1_1);
curl_setopt ($ch, Curlopt_httpheader,array ("Client_ip:". $ip. "," REAL_IP: ". $ip.", "X_forward_for:"
. $ip. "", " X-forwarded-for: ". $ip." "," Proxy_user: ". $ip."));
curl_setopt ($ch, Curlopt_url, "http://192.168.1.99/11.php");
echo curl_exec ($ch);
Curl_close ($ch);
? >


Advanced article:

Curlopt_proxy whether this parameter really uses the proxy to request the data. What is the difference between him and the camouflage IP? Let's do a test here.

curl_proxy_test.php

<?php define (' Is_proxy ', true); Whether to enable the proxy/* Cookie File/* * $cookie _file = dirname (__file__). "/cookie_". MD5 (basename (__file__)). ". txt"; Set cookie file save path and filename/* Analog Browser * * * $user _agent = "mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.2. NET CLR 1.1.4322) ";	

$proxy = "61.135.169.125"; function Vlogin ($url, $data) {//Impersonate login Get cookie function $curl = Curl_init ()//Start a Curl session if (Is_proxy) {//The following code set Proxy Server/
	/proxy server address curl_setopt ($curl, Curlopt_proxy, $GLOBALS [' Proxy ']); }//IP camouflage $ip = rand (1,255). "." Rand (1,255). ".". Rand (1,255). ".".  
	Rand (1,255). "";  curl_setopt ($curl, Curlopt_httpheader, Array (' X-forwarded-for: ', $ip, ' Client-ip: '. $ip));   Construct IP curl_setopt ($curl, Curlopt_referer, "http://www.nowamagic.net/");	
	
	Tectonic antecedents curl_setopt ($curl, Curlopt_header, 1); curl_setopt ($curl, Curlopt_url, $url); Address to be accessed curl_setopt ($curl, Curlopt_ssl_verifypeer, 0); Inspection of the source of certification certificate ($curl, curlopt_ssl_verifyhost, 1); curl_setopt Check the SSL encryption algorithm for the existence of Curl_setop from the certificateT ($curl, curlopt_useragent, $GLOBALS [' user_agent ']); Simulated user-used browser @curl_setopt ($curl, curlopt_followlocation, 1); Use automatic jump curl_setopt ($curl, Curlopt_autoreferer, 1); Automatically set Referer curl_setopt ($curl, Curlopt_post, 1); Send a regular POST request curl_setopt ($curl, Curlopt_postfields, $data); Post-Submitted packet curl_setopt ($curl, Curlopt_cookiejar, $GLOBALS [' cookie_file ']); The name of the file that holds the cookie information curl_setopt ($curl, Curlopt_cookiefile, $GLOBALS [' cookie_file ']); Read the cookie information stored above curl_setopt ($curl, Curlopt_timeout, 30); Set timeout limit to prevent dead loop curl_setopt ($curl, Curlopt_header, 0); Displays the contents of the header area returned curl_setopt ($curl, Curlopt_returntransfer, 1); The information obtained is returned in the form of a file stream $tmpInfo = curl_exec ($curl);
	Execute action if (Curl_errno ($curl)) {echo ' Error '. Curl_error ($curl); } curl_close ($curl); Closes the curl session return $tmpInfo;
Return Data} $params = "username=dudu&password=****";
$result = Vlogin ("http://demo2.test.com/curl_proxy_test2.php", $params); echo $result;; 

curl_proxy_test2.php

<?php
error_reporting ("E_all ^" (E_notice | e_warning) ");
function Getclientip () {  
	if (!empty ($_server["Http_client_ip"))  
		$ip = $_server["Http_client_ip"];  
	else if (!empty ($_server["http_x_forwarded_for"]))  
		$ip = $_server["Http_x_forwarded_for"];  
	else if (!empty ($_server["REMOTE_ADDR"]))  
		$ip = $_server["REMOTE_ADDR"];  
	else  
		$ip = "err";
	return $ip;  
}
echo "<br/>ip:". Getclientip (). "<br/>";  
Print_r ($_server);  
? >

Execute http://demo2.test.com/curl_proxy_test.php

Output: Error couldn ' t connect to host

Indicates that you must have a proxy server to complete the operation. When it comes to the difference between disguise and agent, the disguise will be detected, only use anonymous agent to achieve the real agent effect.

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.