我的 PHP 代码仅在命令行中调用时才有效

问题描述 投票:0回答:1

我有一个带有代理的curl请求

<?php
$botToken = "";
$chatId = "-1234";
$message = 'Test';
$data = [
    'chat_id' => $chatId,
    'text' => $message,
];
$url = "https://api.telegram.org/bot:token/sendMessage";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_HEADER, "Content-type: application/x-www-form-urlencoded\r\n");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_PROXY, 'example.proxy.com:1716');
curl_setopt($ch, CURLOPT_PROXYPORT, '1716');
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'passwd');
curl_setopt($ch, CURLOPT_POSTFIELDS,($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$result = curl_exec($ch);
if(curl_errno($ch)){
    echo 'Curl error: ' . curl_error($ch);
}else{
    echo "No Error";
}
curl_close($ch);
echo $result;
?>

当我在浏览器中运行代码时,它不会连接:

Curl error: Failed to connect to example.proxy.com port 1716: Connection refused

但是当我从命令行运行时:

php aaa.php

工作没有问题。

有什么问题吗?

php linux curl server proxy
1个回答
0
投票

通过在直接管理防火墙中临时允许 ip:port 解决了问题

© www.soinside.com 2019 - 2024. All rights reserved.