Solucionado (ver solução)
Solucionado
(ver solução)
8
respostas

HP Fatal error: Uncaught GuzzleHttp\Exception\ConnectException: Connection refused for URI

Pessoal alguém me ajuda, eu já vi e segui os passos aqui relatados.


<?php

require "vendor/autoload.php"; use GuzzleHttp\Client; use Symfony\Component\DomCrawler\Crawler;

$client = new Client(); $resposta =$client->request('GET', 'https://www.alura.com.br/cursos-online-programacao/php', ['verify' => false]);

$html = $resposta->getBody();

$crawler = new Crawler(); $crawler->addHtmlContent($html);

$cursos = $crawler->filter('span.card-curso__nome');

foreach ($cursos as $curso){ echo $curso->textContent.PHP_EOL; }

8 respostas

A máquina onde você está executando o código é a sua mesmo, ou algum container ou máquina virtual? Possui acesso a internet sem nenhum tipo de restrição na rede através de proxies ou algo do tipo?

É minha mesmo, o script estava rodando normalmente e quando atualizei para php 8 deu isso, voltei a versão para 7.4.12 e continua. Possui acesso a internet e não tem restrição nenhuma.

Luciano, você consegue postar um print com o erro completo, por favor? Ainda não sei o que pode estar causando isso...

C:\php\php.exe E:\Estudos\php\php-composer\buscar-cursos.php
PHP Fatal error:  Uncaught GuzzleHttp\Exception\ConnectException: Connection refused for URI https://www.alura.com.br/cursos-online-programacao/php in E:\Estudos\php\php-composer\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php:311
Stack trace:
#0 E:\Estudos\php\php-composer\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php(230): GuzzleHttp\Handler\StreamHandler->GuzzleHttp\Handler\{closure}()
#1 E:\Estudos\php\php-composer\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php(322): GuzzleHttp\Handler\StreamHandler->createResource(Object(Closure))
#2 E:\Estudos\php\php-composer\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php(58): GuzzleHttp\Handler\StreamHandler->createStream(Object(GuzzleHttp\Psr7\Request), Array)
#3 E:\Estudos\php\php-composer\vendor\guzzlehttp\guzzle\src\PrepareBodyMiddleware.php(35): GuzzleHttp\Handler\StreamHandler->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#4 E:\Estudos\php\php-composer\vendor\guzzlehttp\guzzle\src\Middleware.php(31): GuzzleHttp\PrepareBodyMiddleware->__invoke(O in E:\Estudos\php\php-composer\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php on line 72

Fatal error: Uncaught GuzzleHttp\Exception\ConnectException: Connection refused for URI https://www.alura.com.br/cursos-online-programacao/php in E:\Estudos\php\php-composer\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php:311
Stack trace:
#0 E:\Estudos\php\php-composer\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php(230): GuzzleHttp\Handler\StreamHandler->GuzzleHttp\Handler\{closure}()
#1 E:\Estudos\php\php-composer\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php(322): GuzzleHttp\Handler\StreamHandler->createResource(Object(Closure))
#2 E:\Estudos\php\php-composer\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php(58): GuzzleHttp\Handler\StreamHandler->createStream(Object(GuzzleHttp\Psr7\Request), Array)
#3 E:\Estudos\php\php-composer\vendor\guzzlehttp\guzzle\src\PrepareBodyMiddleware.php(35): GuzzleHttp\Handler\StreamHandler->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#4 E:\Estudos\php\php-composer\vendor\guzzlehttp\guzzle\src\Middleware.php(31): GuzzleHttp\PrepareBodyMiddleware->__invoke(O in E:\Estudos\php\php-composer\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php on line 72

Process finished with exit code 255
solução!

Luciano, isso parece ser algum problema com sua extensão curl.

Você consegue compartilhar seu código todo no GitHub só pra eu garantir?

Se for isso mesmo, sugiro remover a instalação do PHP e refazê-la, já que o Windows pode ser chatinho com alguns arquivos ficando pra trás. =/

Consegui resolver deletando as variáveis de ambiente e removendo o php e reinstalando ele novamente. Obrigado pela dica.

Estou com o mesmo problema, só que no meu caso estou usando Ubuntu 20.04.

Como resolver no caso do Ubuntu?

gustavo@gustavo-Spin-SP314-51:/opt/lampp/htdocs/php/8-ComposerDependencias$ php buscar-cursos.php

Fatal error: Uncaught GuzzleHttp\Exception\ConnectException: Connection refused for URI https://www.alura.com.br/cursos-online-programacao/php in /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php:311
Stack trace:
#0 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php(230): GuzzleHttp\Handler\StreamHandler->GuzzleHttp\Handler\{closure}()
#1 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php(322): GuzzleHttp\Handler\StreamHandler->createResource(Object(Closure))
#2 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php(58): GuzzleHttp\Handler\StreamHandler->createStream(Object(GuzzleHttp\Psr7\Request), Array)
#3 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php(35): GuzzleHttp\Handler\StreamHandler->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#4 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/Middleware.php(31): GuzzleHttp\PrepareBodyMiddleware->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#5 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/RedirectMiddleware.php(71): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Request), Array)
#6 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/Middleware.php(63): GuzzleHttp\RedirectMiddleware->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#7 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/HandlerStack.php(75): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Request), Array)
#8 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/Client.php(331): GuzzleHttp\HandlerStack->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#9 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/Client.php(168): GuzzleHttp\Client->transfer(Object(GuzzleHttp\Psr7\Request), Array)
#10 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/Client.php(187): GuzzleHttp\Client->requestAsync('GET', Object(GuzzleHttp\Psr7\Uri), Array)
#11 /opt/lampp/htdocs/php/8-ComposerDependencias/buscar-cursos.php(9): GuzzleHttp\Client->request('GET', 'https://www.alu...')
#12 {main}

Next GuzzleHttp\Exception\ConnectException: Connection refused for URI https://www.alura.com.br/cursos-online-programacao/php in /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php:72
Stack trace:
#0 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php(35): GuzzleHttp\Handler\StreamHandler->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#1 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/Middleware.php(31): GuzzleHttp\PrepareBodyMiddleware->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#2 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/RedirectMiddleware.php(71): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Request), Array)
#3 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/Middleware.php(63): GuzzleHttp\RedirectMiddleware->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#4 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/HandlerStack.php(75): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Request), Array)
#5 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/Client.php(331): GuzzleHttp\HandlerStack->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#6 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/Client.php(168): GuzzleHttp\Client->transfer(Object(GuzzleHttp\Psr7\Request), Array)
#7 /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/Client.php(187): GuzzleHttp\Client->requestAsync('GET', Object(GuzzleHttp\Psr7\Uri), Array)
#8 /opt/lampp/htdocs/php/8-ComposerDependencias/buscar-cursos.php(9): GuzzleHttp\Client->request('GET', 'https://www.alu...')
#9 {main}
  thrown in /opt/lampp/htdocs/php/8-ComposerDependencias/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php on line 72

Gustavo, baseado em outra dúvida que vc postou, te falta a extensão openssl instalada. ;-)