Problem with httpClient

Hello everyone

I have problem with httpClient

this is my code

        $http = new Client();
        $response = $http->get('http://localhost:8000/10_ARetenir/index.html');
        dd($response);

this the result of dump

^ Cake\Http\Client\Response {#344 ▼
  #code: 502
  #cookies: null
  #reasonPhrase: "cannotconnect"
  #_xml: null
  #_json: null
  #_cookies: []
  #headers: array:5 [▼
    "Content-Type" => array:1 [▼
      0 => "text/html"
    ]
    "Cache-Control" => array:1 [▼
      0 => "no-cache"
    ]
    "Content-Length" => array:1 [▼
      0 => "3425"
    ]
    "X-Frame-Options" => array:1 [▼
      0 => "deny"
    ]
    "Proxy-Connection" => array:1 [▼
      0 => "Keep-Alive"
    ]
  ]
  #headerNames: array:5 [▶]
  -protocol: "1.1"
  -stream: Laminas\Diactoros\Stream {#349 ▶}
}

There is nothing wrong with your code but it seems more like PHP wants to connect to your http://localhost:8000 but doesn’t get a correct anwser.

#code: 502 means Bad Gateway

Have you tried to call http://127.0.0.1:8000 instead of http://localhost:8000 ?
Sometimes VPNs or other security software blocks localhost

1 Like