0

I try to scrape a websites HTML code with a guzzle http-request. In the browser (chrome) everything works fine, but when I try to execute the PHP script with the console (windows) I get the following error:

GuzzleHttp\Exception\ConnectException: Connection refused for URI https://example.com

$httpClient = new \GuzzleHttp\Client();
$response = $httpClient->get("https://example.com");
$htmlString = (string) $response->getBody();
  • I downloaded "php-7.4.23-nts-Win32-vc15-x64.zip" and extracted it I
  • navigated to the folder where I extracted php I ran

php c:\myproject\index.php

Mathis Hüttl
  • 127
  • 3
  • 18
  • are you writing localhost or a website name inplace in uri? – bhucho Aug 31 '21 at 20:43
  • no a real "external" uri – Mathis Hüttl Sep 02 '21 at 20:36
  • then it is most probably due to the website you are trying to access has blocked the users as from where the requests are coming, have you tried sending a simple curl request from the terminal(cmd prompt in case of win) – bhucho Sep 03 '21 at 09:30
  • I am having the same problem. Tested curl on cmd and it works fine. Also accessed the uri in the browser and it also works fine. So it is not server blocking of any sort. – Francisco Luz Feb 23 '22 at 02:38
  • I am having the exact same problem... with the difference that in my dev machine the php code works perfectly, but fails on my customer's server :( – VictorEspina Mar 02 '23 at 15:28

1 Answers1

0

I was having the exact same problem, with the difference that my PHP code worked perfectly on my dev machine... so I decided to check if I was using the same PHP version on both machines and it turned out that I had 7.4.20 on my dev machine and 7.4.33 on the server... so I decided to install my version on the server and... voilla!! problem solved!! I HATE this kind of issues, because they are almost impossible for customers to understand why my code won't work on their servers.

VictorEspina
  • 352
  • 2
  • 7