当我在 PHP 中使用 cURL 收到错误“连接后来自代理的 HTTP 403”时,我该怎么办


What should I do when I get error "HTTP 403 from proxy after CONNECT" using cURL in PHP?

我想使用 Telegram API 查看我的消息,例如"/getupdates 或"/getme",我读到我必须使用 cURL,但不幸的是我在页面上看不到任何内容......空白页。那么我的代码在这里吹有什么问题呢?

<?php
$botToken = "172894271:****myTelegramBotId****";
$botUrl  = "https://api.telegram.org/bot" . $botToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $botUrl."/geupdates");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
//url error: Received HTTP code 403 from proxy after CONNECT
if(curl_errno($ch)) { echo 'Curl error: ' . curl_error($ch);} 
echo $result;
?>
错误

是:"url 错误:连接后从代理接收 HTTP 代码 403"

如果启用了错误报告,您应该会收到许多警告。

例如,这个:

Warning: curl_setopt() expects exactly 3 parameters, 2 given in ..'index.php on line X

对于此行,具体而言:

<?php
error_reporting(E_ALL);
ini_set("display_errors",1);
...
curl_setopt($ch, CURLOPT_RETURNTRANSFER);

另外,$handle从何而来?应该是$ch的,不是吗?

在 apache/conf/extra 中,将有文件 httpd_proxy.conf,设置

ProxyRequests On

并在下面添加:

AllowCONNECT port[-port] [port[-port]]

例如:

ProxyRequests On
AllowCONNECT 443 112 1002

1002是目标端口,重新启动Apache并尝试