Guzzle帖子无法使用外部ip。抛出 Failed to connect to 37.XX.XX.XXX port 80: Connection timed out(连接超时)。

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

我知道这个问题有很多答案,但是没有用。

受影响的Guzzle版本: 6.3

PHP版本:7.2.24

cURL版本: 7.58.0

在laravel项目中使用guzzle,在本地IP下一切正常。但当使用外部(静态)IP时,它显示以下错误连接失败到37.XX.XX.XXX 80端口:连接已超时

虽然当相同的URL直接用于浏览器或邮递员,那么它与外部IP工作。我已经做了一个谷歌和尝试,但没有工作。

我已经尝试了与普通的curl也。下面是代码。

$postData = [
            'grant_type'    => 'password',
            'client_id'     => 10,
            'client_secret' => 'xHuxxxxxxxflw',
            'username'      => '[email protected]',
            'password'      => 'abc',
            'scope'         => '*',
        ];
        $ch     = curl_init();
        $url = 'http://37.XX.XX.XXX/oauth/token';
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_HEADER, false); 
        curl_setopt($ch, CURLOPT_POST, count($postData));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

        $data = curl_exec($ch);
        $curl_errno = curl_errno($ch);
        $curl_error = curl_error($ch);
        if ($curl_errno > 0) {
                echo "cURL Error ($curl_errno): $curl_error\n";
        } else {
                echo "Data received\n";
        }
        curl_close($ch);

        echo ($data);

显示我下面的错误

cURL Error (7): Failed to connect to 37.XX.XX.XXX port 80: Connection timed out

谁能帮帮我?我花了很多时间去解决,但是什么都不行。

php laravel guzzle
1个回答
0
投票

问题已经通过允许出站端口解决了。出站端口不允许,所以无法连接请求。

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