帮助文档>代理IP>代码示例 > 代码示例-PHP

代码示例-PHP

发布时间:2021-08-03 21:05

PHP curl

  1. /**
  2. * 请求
  3. *
  4. * @param [type] $targetUrl 目标站点
  5. * @param [type] $proxyIp 代理ip
  6. * @param [type] $proxyPort 代理端口
  7. * @param [type] $proxyUser AuthKey
  8. * @param [type] $proxyPassword AuthPwd
  9. * @return void
  10. */
  11. function sendRequest($targetUrl, $proxyIp, $proxyPort, $proxyUser, $proxyPassword){
  12. $ch = curl_init();
  13. curl_setopt($ch, CURLOPT_URL, $targetUrl);
  14. curl_setopt($ch, CURLOPT_HEADER, 0);
  15. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  16. curl_setopt($ch, CURLOPT_PROXYPORT, $proxyPort);
  17. curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
  18. curl_setopt($ch, CURLOPT_PROXY, $proxyIp);
  19. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyUser . ':' . $proxyPassword);
  20. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  21. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  22. $data = curl_exec($ch);
  23. curl_close($ch);
  24. return $data;
  25. }
  26. $data = sendRequest('https://ip.cn/api/index?ip=&type=0', '请改成您的代理IP', 端口号, '请改成您的Key', '请改成您的AuthPwd');
  27. var_dump($data);

PHP stream

  1. /**
  2. * 请求
  3. *
  4. * @param [type] $targetUrl 目标站点
  5. * @param [type] $proxyIp 代理ip
  6. * @param [type] $proxyPort 代理端口
  7. * @param [type] $proxyUser AuthKey
  8. * @param [type] $proxyPassword AuthPwd
  9. * @return void
  10. */
  11. function sendRequest($targetUrl, $proxyIp, $proxyPort, $proxyUser, $proxyPassword){
  12. $proxyAuth = base64_encode($proxyUser . ":" . $proxyPassword);
  13. $headers = implode("\r\n", [
  14. "Proxy-Authorization: Basic {$proxyAuth}"
  15. ]);
  16. // 目标站为https时需要
  17. // $sniServer = parse_url($targetUrl, PHP_URL_HOST);
  18. $options = [
  19. "http" => [
  20. "proxy" => $proxyIp . ':' . $proxyPort,
  21. "header" => $headers,
  22. "method" => "GET",
  23. "request_fulluri" => true,
  24. ],
  25. // 目标站为https时需要
  26. // 'ssl' => array(
  27. // 'SNI_enabled' => true,
  28. // 'SNI_server_name' => $sniServer
  29. // )
  30. ];
  31. $context = stream_context_create($options);
  32. $result = file_get_contents($targetUrl, false, $context);
  33. return $result;
  34. }
  35. $data = sendRequest('https://ip.cn/api/index?ip=&type=0', '请改成您的代理IP', 端口号, '请改成您的Key', '请改成您的AuthPwd');
  36. var_dump($data);

PHP GuzzleHttp

  1. /**
  2. * 请求
  3. *
  4. * @param [type] $targetUrl 目标站点
  5. * @param [type] $proxyIp 代理ip
  6. * @param [type] $proxyPort 代理端口
  7. * @param [type] $proxyUser AuthKey
  8. * @param [type] $proxyPassword AuthPwd
  9. * @return void
  10. */
  11. function sendRequest($targetUrl, $proxyIp, $proxyPort, $proxyUser, $proxyPassword){
  12. $client = new \GuzzleHttp\Client();
  13. $proxyAuth = base64_encode($proxyUser . ":" . $proxyPassword);
  14. $options = [
  15. "proxy" => $proxyIp . ':' . $proxyPort,
  16. "headers" => [
  17. "Proxy-Authorization" => "Basic " . $proxyAuth
  18. ]
  19. ];
  20. $result = $client->request('GET', $targetUrl, $options);
  21. return $result->getBody()->getContents();
  22. }
  23. $data = sendRequest('https://ip.cn/api/index?ip=&type=0', '请改成您的代理IP', 端口号, '请改成您的Key', '请改成您的AuthPwd');
  24. var_dump($data);
本文导读

客户热线: