PHP 代理

简介: PHP 代理

PHP 中的代理是通过 cURL 库创建的。cURL 表示客户端 URL 是 php 中最强大的扩展之一。

Denial Stenberg 创建了 cURL 库来在不同服务器之间进行通信。

cURL 具有可以通过不同的 IP 和端口发送请求的功能。cUrl 允许我们通过 URL 发送和接收数据。

本教程演示如何在 PHP 中启用 cURL 并使用 cURL 创建代理。

[在 PHP 中启用 cURL 库]

在开始创建代理之前,我们需要启用 cURL 库。cURL 库已经存在于 PHP 中,我们必须启用它。

首先,我们需要检查是否启用了 cUrl。创建一个 info.php 文件并运行它。

<?php
    phpinfo();
?>

当你运行这个文件时,它会显示 PHP 的所有信息。在进行下一步之前,有必要为 PHP 设置 PATH 变量。

如果已设置,则可以继续。在 info.php 上搜索 cURL。

如果未启用,请转到 PHP 主文件夹并找到 php.ini 文件。最有可能的路径是 C:\php74\php.ini

使用文本编辑器打开 php.ini 文件。搜索 curl,你会发现类似这样的内容:

;extension=php_curl.dll

或者对于较新的 PHP 版本:

;extension=curl

现在删除 ; 在扩展之前。保存 php.ini 文件并退出,重新启动你正在使用的服务器或本地主机。

extension=cur

再次运行 info.php 并搜索 cURL support。你会看见:

cURL support: enabled

这意味着 cURL 已启用,你可以开始了。如果 cURL 仍未启用,请在扩展后尝试 php_curl.dll 的绝对路径。

extension=C:/php74/ext/php_curl.dll

[使用 cURL 和 PHP 创建代理]

cURL 有许多不同的关键字用于不同的操作。让我们看一个为给定 URL 创建代理请求的示例:

//URL you want to apply cURL proxy.
$site = 'http://google.com';
// The Proxy IP address
$proxy_ip = '138.117.84.240';
//The proxy port.
$proxy_port = '999';
//Authentication information for proxy, username and password.
$Username = 'demouser';
$Password = 'demopassword';
//Initiate cURL response
$curl_reponse = curl_init($site);
curl_setopt($curl_reponse, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_reponse, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl_reponse, CURLOPT_HTTPPROXYTUNNEL , 1);
//Setting the IP address for proxy.
curl_setopt($curl_reponse, CURLOPT_PROXY, $proxy_ip);
//Setting the port for proxy.
curl_setopt($curl_reponse, CURLOPT_PROXYPORT, $proxy_port);
//Specifying the authentication information.
curl_setopt($curl_reponse, CURLOPT_PROXYUSERPWD, "$Username:$Password");
//Define the proxy server type, it is not neccessary, incase there is proxy connection aborted error. 
//The defaults type is CURLPROXY_HTTP, and the other options are CURLPROXY_SOCKS4, CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5,and CURLPROXY_SOCKS5_HOSTNAME.
curl_setopt($curl_reponse, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
//Execute the proxy request.
$output = curl_exec($curl_reponse);
//All the errors with number are given at this link from PHP Manual https://www.php.net/manual/en/function.curl-errno.php
//Error Number
echo curl_errno($curl_reponse).'<br/>';
// Error Info
echo curl_error($curl_reponse).'<br/>';
//Show the output, Return a cURL handle on success, and FALSE if there is an error.
echo $output;

上面的代码是创建代理请求以运行 google.com 的设置。该代码使用代理身份验证。

代理不需要连接到给定的 IP 和端口。如果代理连接成功,它将返回 true 或 cURL 句柄。

如果有任何错误,它将打印错误号和信息并返回 false。请参阅输出以了解最常见的错误之一:

28
Failed to connect to 138.117.84.240 port 999: Timed out
相关文章
|
应用服务中间件 PHP 开发工具
Nginx 后端代理多台 php-fpm 服务器
Nginx 后端代理多台 php-fpm 服务器
|
Unix 应用服务中间件 PHP
nginx 后端代理多台 php-fpm 服务器;(补充上一篇博客)
nginx 后端代理多台 php-fpm 服务器;(补充上一篇博客)
|
PHP
php pecl设置代理,php pear设置代理
php pecl设置代理,php pear设置代理
266 0
|
Java 应用服务中间件 PHP
CentOS7 nginx+tomcat实现代理访问java web项目让项目支持jsp和php
首先配置:nginx # cd /etc/nginx/conf.d/ # cp default.conf tomcat.conf # nano tomcat.conf 请在tomcat.
1586 0
|
应用服务中间件 PHP nginx