注册
登录
 文档中心 产品介绍 开发指南 API接口 代码样例 使用帮助
使用PHP通过API提取链接获取代理IP的代码样例
站大爷 官方 2024-01-20 1808 浏览

温馨提示:

1.  以下样例分别为Guzzle、使用stream流、curl通过调用API提取链接获取代理IP

2.  Guzzle是一个简单强大的http客户端库,需要安装才能使用:

       安装composer curl -sS https://getcomposer.org/installer | php

       安装Guzzle php composer.phar require guzzlehttp/guzzle:~6.0

3.  站大爷API提取链接在控制台中"实例管理"中生成


Guzzle

<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;

//api链接
$api_url = "http://www.***.com/ShortProxy/GetIP/?api=1234567890&akey=8a17ca305f683620&count=10&timespan=3&type=3";

$client = new Client();
$res = $client->request('GET', $api_url, [
	'headers' => [
		'Accept-Encoding' => 'gzip'  // 使用gzip压缩让数据传输更快
	]
]);


echo $res->getStatusCode(); //获取Reponse的返回码
echo "\n\n";
echo $res->getBody(); //获取API返回内容
?>


使用stream流

<?php

// gzip解压缩函数
if (!function_exists('gzdecode')) {
	function gzdecode($data) {
		// strip header and footer and inflate
		return gzinflate(substr($data, 10, -8));
	}
}

//api链接
$api_url = "http://www.***.com/ShortProxy/GetIP/?api=1234567890&akey=8a17ca305f683620&count=10&timespan=3&type=3";

$opts = array('http' =>
	array(
		'method'  => 'GET',
		'header'  => 'Accept-Encoding: gzip',  // 使用gzip压缩让数据传输更快
	)
);

$context = stream_context_create($opts);

$result = file_get_contents($api_url, false, $context);

echo gzdecode($result);  // 输出返回内容
?>


curl

<?php
	//api链接
	$api_url = "http://www.***.com/ShortProxy/GetIP/?api=1234567890&akey=8a17ca305f683620&count=10&timespan=3&type=3";

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $api_url);

	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

	curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //使用gzip压缩传输数据让访问更快

	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
	curl_setopt($ch, CURLOPT_TIMEOUT, 5);

	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	$result = curl_exec($ch);
	$info = curl_getinfo($ch);
	curl_close($ch);

	echo $result;
	echo "\n\nfetch ".$info['url']."\ntimeuse: ".$info['total_time']."s\n\n";
?>


立即注册站大爷用户,免费试用全部产品
立即注册站大爷用户,免费试用全部产品