-
产品介绍
-
购买指南
-
使用指南
-
快速入门
-
电脑如何使用代理IP
-
手机如何使用代理IP
-
浏览器如何使用代理IP
-
软件如何使用代理IP
-
代码调用方式示例
-
常见问题
PHP语言代码示例
发布时间:2021-12-08 16:15
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://myip.top', '请改成您的代理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://myip.top', '请改成您的代理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://myip.top', '请改成您的代理IP', 端口号, '请改成您的Key', '请改成您的AuthPwd');
var_dump($data);
相关文章
微信客服