[ 登录 ] - [ 注册 ] | 免费代理IP 数据无忧_无忧代理IP_DATA5U最新上线 | 代码示例DEMO | IP测试视频 | 用户协议
PHP(版本5.4.45)代码调用示例·Https代理(API)

/**
 * 请将下面代码另存为PHP文件,填入您的订单号,在浏览器访问即可
**/

error_reporting(0); //抑制所有错误信息

header("Content-type:text/html;charset=utf-8");  //设置页面编码为UTF-8
echo '<body style="font-size: 13px; line-height: 30px;">';

function curl_string($url,$user_agent,$proxy, $charset){
    $ch = curl_init();
			
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
	curl_setopt($ch, CURLOPT_SSLVERSION, 2);//设置SSL协议版本号,可选1,2,3;具体参考http://www.data5u.com/help/article-69.html
    
    curl_setopt ($ch, CURLOPT_PROXY, $proxy); //设置代理IP
    curl_setopt ($ch, CURLOPT_URL, $url); 
    curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent); //设置UA
    curl_setopt ($ch, CURLOPT_HEADER, 0); //设置为0、1控制是否返回请求头信息
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); //启动跳转链接
    curl_setopt ($ch, CURLOPT_TIMEOUT, 30);  //设置超时时间为30秒
    curl_setopt($cs, CURLOPT_COOKIEJAR, "c:\cookie.txt"); // 设置Cookies,如果没有请把此行注释
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept-encoding:gzip, deflate, sdch, br', 'origin:http://www.baidu.com')); // 设置Header 
    curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com/"); // 设置Referer
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);  //多级自动跳转
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');    // GET请求
    
    $result = curl_exec($ch);
    curl_close($ch);
    return mb_convert_encoding($result, "utf-8", $charset);
}

$order = 'please-input-your-order-here';  // 请填写爬虫(动态)代理订单号,在用户中心获取
$api = 'http://api.ip.data5u.com/dynamic/get.html?order=' . $order; // 拼接API接口
$requestUrl = 'http://ip.chinaz.com/getip.aspx';  // 请求的目标网址
$ua = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 OPR/43.0.2442.1144';

echo '* 调用API接口 : ' . $api . '<br>';

$iplist  = curl_string($api, '', '', 'UTF-8'); 
$ips = explode('\n',$iplist);
echo '* 获取到IP数量 : ' . count($ips) . '条<br>';

$useip = $ips[array_rand($ips, 1)]; // 随机获取一个代理IP
	
echo '* 使用代理IP : ' . $useip . ' 抓取网址: ' . $requestUrl . '<br>';
	
$file_contents = curl_string($requestUrl, $ua, 'http://' . $useip, 'GB2312'); 
echo '* 返回的HTML源码为 : <br><textarea style="width: 100%; height: 90px; display:block;">' . $file_contents . '</textarea>';

echo '</body>';


微信公众号
关注微信公众号