已解决:网站部署后可以正常访问,但是用curl请求时总是返回false。解决办法:在服务器进行域名内网ip解析。

gongaiorg

问题描述

问题已解决:
是因为云主机的问题,需要在本地进行解析,而且不能用公网ip,要用127.0.0.1
非常感谢大佬的指导,解决了这个困扰大半天的困惑,大佬一语道破,解决问题,非常感谢!!!

1、部署在linux服务器上了,并且是两个不同站点的网址请求,并非同一个进程

2、用webman框架,网站部署后可以正常访问 https://push.21c.xin/ ,但是用curl请求网站时总是返回false,

3、同样的代码,更换请求其它网址可以正常返回值,这是服务器的配置问题吗?怎么修改呢,找了很多方案都不行,特请教大佬!

测试网址:https://sso.21c.xin/passport/index/curl?url=https://baidu.com 可以正常返回百度的首页内容
var_dump($tmpInfo); 返回html源码
var_dump(curl_errno($curl)); 返回0
var_dump(curl_getinfo($curl, CURLINFO_HTTP_CODE)); 返回200

curl访问 网址 https://sso.21c.xin/passport/index/curl?url=https://push.21c.xin/ 却返回为false
var_dump($tmpInfo); 返回false
var_dump(curl_errno($curl)); 返回52
var_dump(curl_getinfo($curl, CURLINFO_HTTP_CODE)); 返回0

    public function curl(  ): Response
    { 
        $curl = new Curl(); 
        $url =request()->get('url','https://sso.21c.xin');
        $result = $curl->get($url); 
        var_dump($result);
        return json($result);
    } 

1298 4 0
4个回答

不败少龙

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'X-AjaxPro-Method:ShowList',
        'Content-Type: application/json; charset=utf-8',
        'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$data = curl_exec($ch);
curl_close($ch);
return json_decode($data, true);
  • gongaiorg 2023-04-23

    我写的get请求,请求其它网站都可以正常返回,但是请求webman部署的网站不行,都是返回false

  • 不败少龙 2023-04-23
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
  • gongaiorg 2023-04-23

    https://sso.21c.xin/passport/index/curl1?url=https://baidu.com 用你代码写了这个是正常可以访问的
    但是https://sso.21c.xin/passport/index/curl1?url=https://push.21c.xin/ 还是返回false

    应该是服务器设置的问题吗

  • 不败少龙 2023-04-23

    是不是对方的网站请求头需要设置什么的

  • gongaiorg 2023-04-23

    请求的是我自己的服务器,现在做微服务架构,把模块分离了,不知道服务器要怎么设置才能解决这个问题呢

  • 不败少龙 2023-04-23

    http_get('https://push.21c.xin/') 我这直接请求你的这个 可以获取到值啊

  • gongaiorg 2023-04-23

    你怎么写的代码啊
    public function curl1( )
    {
    $url = input('url')?:'https://sso.21c.xin';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    var_dump($result);
    return json($result);
    }
    刚按照你发的代码也是不可以呢,返回依然是false

  • 不败少龙 2023-04-23

    $url =request()->get('url','https://sso.21c.xin'); 这杨写呢

tanhongbin

截图,这能访问就怪了

智佳思远

提问把系统环境带上啊。如果是windows开发,windows是单进程的,就一个进程不能自己请求自己

  • gongaiorg 2023-04-23

    我部署在linux服务器上了,并且是两个不同的网址请求,并非同一个进程

  • 智佳思远 2023-04-23

    php start.php status 截图下

  • gongaiorg 2023-04-23

    已经发到问题下面了

  • 智佳思远 2023-04-23

    我这浏览器都没办法访问你的域名,显示超时,telnet也连接超时。

    telnet push.21c.xin 443
    Trying 43.154.4.5...
    ^C
    
    telnet sso.21c.xin 443
    Trying 43.154.4.5...
    ^C

    你的服务器估计也无法访问你的域名,在你服务器上telnet sso.21c.xin 443 试下吧。看看是不是使用了代理 cdn什么的

  • gongaiorg 2023-04-23

    不应该吧,push.21c.xin 和 sso.21c.xin 就解析在这个43.154.4.5服务器上

  • gongaiorg 2023-04-23

    [root@mail sso.21c.xin]# telnet sso.21c.xin
    -bash: telnet: command not found
    [root@mail sso.21c.xin]# ping sso.21c.xin
    PING hk1.domainagent.pub (43.154.4.5) 56(84) bytes of data.

  • 智佳思远 2023-04-23

    ping和telnet是不同的协议,一个icmp,一个tcp,ping通不代表tcp能连接,反过来也是。
    apt-get install telnet 或者 yum install telnet 装下telnet命令
    telnet命令是 telnet sso.21c.xin 443,端口号加上

  • gongaiorg 2023-04-23

    [root@mail sso.21c.xin]# telnet sso.21c.xin 443
    Trying 43.154.4.5...
    Connected to sso.21c.xin.
    Escape character is '^]'.

tanhongbin

截图
截图
没有问题呀我这里

  • gongaiorg 2023-04-23

    我用的curl访问的,你看看上面的请求方式哪里不对吗

  • tanhongbin 2023-04-23

    我用的是这个GuzzleHttp\Client ,现在谁还curl 什么年代了???

  • gongaiorg 2023-04-23

    GuzzleHttp\Exception\ConnectException: cURL error 52: Empty reply from server (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://push.21c.xin/

  • gongaiorg 2023-04-23

    我用你说的这个类库,报错52,和curl是一样的

  • tanhongbin 2023-04-23

    你这是电脑问题嘛???

  • tanhongbin 2023-04-23

    我们都能请求,不知道为啥你请求不了

  • gongaiorg 2023-04-23

    应该是服务器配置的问题吧

  • tanhongbin 2023-04-23

    你是不是跨域了???你接口端webman的路由怎么写的??

  • tanhongbin 2023-04-23

    还有你webman那边是不是 nginx代理的,你得看看你得协议 是不是http1.0 的,你用的是http1.1请求的,导致协议不一致

  • gongaiorg 2023-04-23

    curl请求不存在跨域问题吧,协议是1.0

  • gongaiorg 2023-04-23

    location / {
    proxy_pass http://127.0.0.1:9901;
    index index.php index.html error/index.html;
    autoindex off;
    proxy_set_header Host $host;
    proxy_http_version 1.0;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

  • gongaiorg 2023-04-23

    1.1 和1.0 都试过了,都是返回false

  • PHP甩JAVA一条街 2023-04-23

    环境有问题吧?

  • gongaiorg 2023-04-23

    添加host解析 设置到本地127.0.0.1就可以了

年代过于久远,无法发表回答
🔝