代码示例-PHP
发布时间:2021-08-03 21:05
PHP curl
/**
* 请求
*
* @param [type] $targetUrl 目标站点
* @param [type] $proxyIp 代理ip
* @param [type] $proxyPort 代理端口
* @param [type] $proxyUser AuthKey
* @param [type] $proxyPassword AuthPwd
* @return void
*/
function sendRequest($targetUrl, $proxyIp, $proxyPort, $proxyUser, $proxyPassword){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $targetUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxyPort);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_PROXY, $proxyIp);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyUser . ':' . $proxyPassword);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$data = sendRequest('https://ip.cn/api/index?ip=&type=0', '请改成您的代理IP', 端口号, '请改成您的Key', '请改成您的AuthPwd');
var_dump($data);
PHP stream
/**
* 请求
*
* @param [type] $targetUrl 目标站点
* @param [type] $proxyIp 代理ip
* @param [type] $proxyPort 代理端口
* @param [type] $proxyUser AuthKey
* @param [type] $proxyPassword AuthPwd
* @return void
*/
function sendRequest($targetUrl, $proxyIp, $proxyPort, $proxyUser, $proxyPassword){
$proxyAuth = base64_encode($proxyUser . ":" . $proxyPassword);
$headers = implode("\r\n", [
"Proxy-Authorization: Basic {$proxyAuth}"
]);
// 目标站为https时需要
// $sniServer = parse_url($targetUrl, PHP_URL_HOST);
$options = [
"http" => [
"proxy" => $proxyIp . ':' . $proxyPort,
"header" => $headers,
"method" => "GET",
"request_fulluri" => true,
],
// 目标站为https时需要
// 'ssl' => array(
// 'SNI_enabled' => true,
// 'SNI_server_name' => $sniServer
// )
];
$context = stream_context_create($options);
$result = file_get_contents($targetUrl, false, $context);
return $result;
}
$data = sendRequest('https://ip.cn/api/index?ip=&type=0', '请改成您的代理IP', 端口号, '请改成您的Key', '请改成您的AuthPwd');
var_dump($data);
PHP GuzzleHttp
/**
* 请求
*
* @param [type] $targetUrl 目标站点
* @param [type] $proxyIp 代理ip
* @param [type] $proxyPort 代理端口
* @param [type] $proxyUser AuthKey
* @param [type] $proxyPassword AuthPwd
* @return void
*/
function sendRequest($targetUrl, $proxyIp, $proxyPort, $proxyUser, $proxyPassword){
$client = new \GuzzleHttp\Client();
$proxyAuth = base64_encode($proxyUser . ":" . $proxyPassword);
$options = [
"proxy" => $proxyIp . ':' . $proxyPort,
"headers" => [
"Proxy-Authorization" => "Basic " . $proxyAuth
]
];
$result = $client->request('GET', $targetUrl, $options);
return $result->getBody()->getContents();
}
$data = sendRequest('https://ip.cn/api/index?ip=&type=0', '请改成您的代理IP', 端口号, '请改成您的Key', '请改成您的AuthPwd');
var_dump($data);
相关文章