GuzzleHttp\Exception\ConnectException: Connection refused for URI - php

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

Related

Is Endpoint with custom port does not supported by Guzzle?

I am trying to hit API endpoint from my Laravel 5.8.* project.
I am using Guzzle 6.0 with PHP version 7.1.3.
When I run my project in local machine, I get the API response successfully.
But after uploading the project to liver server, I could not get the API response.
I get the response "cURL error 7: Failed to connect to xx.xx.xx.xx port 3333: Connection refused (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)".
The code that I use is as follows.
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'http://xx.xx.xx.xx:3333/api/getrecords', [
'tbl' => ['user', 'activity']
]);
echo $res->getMessage();
The same code is working perfectly in local machine but not in live server.
My Server is Linux server.
Did you have any proxy configured in your server? If so, try to set a proxy in your guzzle request: Guzzle proxy docs
Basically, you have to set an array with the proxy.
$client->request('GET', '/', ['proxy' => 'tcp://localhost:8125']);
you are trying to make request using port 3333. This port 3333 needs to be open in your machine only then you will be able to make request from this port.

Laravel 5.8.* using GuzzleHttp on production

I'm using GuzzHttp in my laravel project to post some data to an API. In my local enviroment I have to to create my client with the cacert.pem file verification like this:
$client = new \GuzzleHttp\Client(['verify' => 'C:\wamp64\bin\php\php7.2.14\cacert.pem']);
But I can't do so on my shared hosting. So, removing that verification:
$client = new \GuzzleHttp\Client();
Results on an internal server error:
[2019-06-11 15:17:10] local.ERROR: GuzzleHttp\Exception\ServerException: Server error: `POST https://pruebas2.mayoristaseguridad.es/tiendaseguridad/02-ConectorLaravelPrestashop/ConectorCategorias.php` resulted in a `500 Internal Server Error` response in /home/admin/web/iberoseguridad.eu/public_html/csv_products/vendor/guzzle/guzzlehttp/guzzle/src/Exception/RequestException.php:113
Is there a way to fix this error?
Not sure if this will help, here it goes anyways
// Disable validation entirely (don't do this!).
$client->request('GET', '/', ['verify' => false]);
Guzzle verify api
This ended up being a remote server error, not mine. I just had to communicate that error with the server's maintainer and now it works just fine.

Curl/Laravel fails when loading resource but not when loaded from browser

We have a route that pulls JSON data from same server that is hosting the front-end but on a different sub domain. The call always fails with a connection refused error. However, pulling the data by calling the resource route directly works without issue.
Here is the failing code.
$client = new Client();
$response = $client->get("http://api.xxxxx.xx./invoice/".$accountNumber."/".$invoiceNumber."/".$date);
$invoicecontent = json_decode($response->getBody()->getContents());
The above always fails with this error:
ConnectException in CurlFactory.php line 186:
cURL error 7: Failed to connect to api.xxx.xx port 80: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
However, opening the failing page in the browser works perfectly.
Server enviroment:
Windows 10
XAMPP
PHP 7
Any suggestions would be appreciated.

php soap client returns 404, when calling WSDL methods from HTTPS environment

I am running a php soap client example under https environment of my Zend Server.
I am able to list the methods in the WSDL. But when i make a call to one of those methods, i receive a 404 HTTP not found error. I also tried saving the WSDL to my local folder.
Interestingly, the same example was running successfully from the CLI (like: C:/> php abc.php). Not sure why it is failing in the browser (like: https://example.com:10022/abc.php).
This is my code:
<?php
ini_set("soap.wsdl_cache_enabled", "0");
try {
$client = new SoapClient('country.wsdl');
//$client = new SoapClient('http://www.webservicex.net/country.asmx?WSDL');
//$functions = $client->__getFunctions (); var_dump ($functions); // works
$params = array('CountryName'=> 'Australia');
$result = $client->GetISOCountryCodeByCountyName($params);
echo $result->GetISOCountryCodeByCountyNameResult;
}
catch(SoapFault $fault) {
echo $fault->getMessage();
}
?>
Note:
I have openssl enabled.
My https url is configured as a Zend server Virtual Host, running on port 10022.
My https url is not reachable from outside world (does this make any difference?)

SOAP and SSL in PHP

I've got a problem with SOAP and SSL.
When I create a SOAP client with normal wsdl (http), it runs ok. But when I connect with a SSL wsdl (https) it's got problem.
I tried the same code to connect to the https wsdl at my localhost, it's OK. But it doesn't work if that code run under my server.
My server is running Centos, PHP 5.3.8. Can you show me how to config PHP to run SOAP with SSL. Below is my current code:
<?php
// where BKTransactionAPI extends SoapClient...
$bk = new BKTransactionAPI("https://www.baokim.vn/services/transaction_api/init?wsdl");
$info_topup = new TopupToMerchantRequest();
$info_topup->card_id = $service;
$info_topup->pin_field = $pin;
$info_topup->transaction_id = $transaction_id;
$result = new TopupToMerchantResponse();
$result = $bk->DoTopupToMerchant($info_topup);
?>

Categories