首先,Telegram 在我的国家被屏蔽了。由于某些原因我无法使用 VPN,所以我尝试连接 Telegram's bot API通过 HTTP/SOCKS5 代理 ngrok .
我测试了以下方法,但均无效:
我在 ngrok 设置文件中设置了
http_proxy
和socks5
,但是 PHP 仍然无法连接到 Telegram API:$url="https://api.telegram.org/botToken/getUpdates"; $json = file_get_contents($url); file_put_contents('json.txt', $json);
结果:
failed to open stream: No connection could be made because the target machine actively refused it
cURL 代理:
$url="https://api.telegram.org/botToken/getUpdates"; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_PROXY, 'socks5://127.0.0.1:9050');// Using tor bundler proxy curl_setopt($ch, CURLOPT_URL,$url); $result=curl_exec($ch); curl_close($ch); var_dump(json_decode($result, true));
返回
NULL
注意:以上脚本仅在我打开 VPN 时有效
有没有人有更好的主意?
最佳答案
使用 'socks5h://127.0.0.1:9050' 而不是 'socks5://127.0.0.1:9050'
curl_setopt($ch, CURLOPT_PROXY, 'socks5h://127.0.0.1:9050')
关于php - 使用 ngrok 通过 HTTP/代理连接到 Telegram Bot API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53914350/