Laravel 5.8.* using GuzzleHttp on production - php

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.

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 Guzzle Could Not Resolve Host

I have a Laravel application which is hosted locally on a local IP.
It will always be hosted internally on the IP.
I'm trying to connect my application to Interlinks API, on sending a $client->post() I get the following error:
ConnectException in CurlFactory.php line 186: cURL error 6: Could not
resolve host: headers (see
http://curl.haxx.se/libcurl/c/libcurl-errors.html)
Below is the code in my controller.
$client = new \GuzzleHttp\Client();
$client->setDefaultOption('headers', array('Accept' => 'application/json', 'Content-Type' => 'application/json', 'GEOSESSION' => 'MTAuMjYuMy43asdasdasdasdTky'));
$body = json_encode($job);
$res = $client->post('https://api.interlinkexpress.com/shipping/shipment',['body' => $body]);
dd($res);
Does anyone have any ideas why I get the could not resolve host? I'm hoping it's nothing to do with being local.
Thanks
Try to use the latest Guzzle (6.2 at the moment).
It seems that you are using older version, because setDefaultOption is not available in 6.x.

Soap client stopped working

I have a website which frequently makes SOAP calls to a particular API. The site was working fine for a few months of time, however the SOAP functionality suddenly stopped working without any known reason, the error was "Cannot connect to host".
The WSDL service has not moved or shut down.
After this I updated plesk from version 12 to version 17. SOAP client is still not working but now its a different error:
SOAP-ERROR: Parsing WSDL: Couldn't load from (URL) : failed to load external entity (URL)
This is my SOAP call:
$opts = array('http'=>array('user_agent' => 'PHPSoapClient'));
$context = stream_context_create($opts);
libxml_disable_entity_loader(false);
$client = new SoapClient($url,array('stream_context' => $context,'cache_wsdl' => WSDL_CACHE_NONE));
Any possible solutions?
EDIT:
New information came in, so the API server had DNS problems, how can I restore my connection to the API?
Your php server is clearly not reaching out the soap server. This might help:
Try checking directly from php server if the service are reachable:
wget {url} > page.txt
nano (or equivalent) page.txt
Try using other program to consume the server, I suggest Soap Ui
On the php server try ping the service url and see if the IP is resolved.
Check if any other soap service is reachable, you can use this service
If this not help you coming up with a solution, post here the results to help others solving it.

Laravel, Local Hosted cURL error 60: SSL certificate

I keep getting the error which calling an API locally, works fine on my server:
cURL error 60: SSL certificate problem: unable to get local issuer
certificate
Is there a way to stop this exception from showing locally?
Here's my code, ignore the () as this is variable info:
public function testCheck($domains){
$client = new Client();
$res = $client->request('GET', 'https://api.namecheap.com/xml.response?ApiUser=(username)&ApiKey=(apikey)&UserName(username)&ClientIp=(ip)&Command=namecheap.domains.check&DomainList=' . $domains);
$data = json_decode($res->getBody());
dd($data);
}
Is there a way to stop this exception from showing locally so I can carry on testing?
If you are testing locally you can disable the SSL verification:
// Disable validation entirely
$client->request('GET', '/', ['verify' => false]);
See the docs for more information:
http://guzzle.readthedocs.io/en/latest/request-options.html#verify-option
However you should not use this for anything else than testing, in normal cases you should use a certificate and verify this certificate properly.

Goutte - TLSv1 protocol version error

I'm using Goutte to get a page on a web server using an SSL certificate. Whenever I try to get this page the the following exception is thrown:
Uncaught exception 'Guzzle\\Http\\Exception\\CurlException' with message
'[curl] 35: error:1407742E:SSL
routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version
[url] https://somesite.com
I have been looking online for this type of error. It seems that this error happens when there is a handshake failure. The server seems to support TLSv1 and the client uses SSL23.
I'm not sure if this assessment is correct nor do I know how to correct it.
This is the code I'm currently using:
<?php
use Goutte\Client;
$client = new Client();
$guzzle = $client->getClient();
$guzzle->setConfig(
array(
'curl.CURLOPT_SSL_VERIFYHOST' => false,
'curl.CURLOPT_SSL_VERIFYPEER' => false,
)
);
$client->setClient($guzzle);
$crawler = $client->request('GET', 'https://somesite.com'); // IT FAILS HERE
UPDATE:
NOTE: I started getting a few weeks ago a similar error, so I thought I would update the question and answer as they are related.
I got a similar error:
[curl] 35: Unknown SSL protocol error in connection to website.com:443
I found the answer to this problem in the blogpost "Fixing SSL Handshake with PHP5 and Curl"
This error can happen in some operating systems but not in all making it a hard issue to replicate. I am currently using Ubuntu 12.04 in my development environment.
Thankfully this is something that can be addressed at the code level like this:
use Goutte\Client;
$client = new Client();
$guzzle = new GuzzleClient('https://somesite.com', array(
'curl.options' => array(
'CURLOPT_SSLVERSION' => 'CURL_SSLVERSION_TLSv1',
)
));
$client->setClient($guzzle);
$crawler = $client->request('GET', 'https://somesite.com');
UPDATE:
For the solution for the message
[curl] 35: Unknown SSL protocol error in connection to website.com:443
was a bit tricky to resolve as the message gives no indication of what is going on.
This GitHub issue pointed me in the right direction tho. There are multiple versions of the CURLOPT_SSLVERSION I can use (of course!), but these include v1.0, v1.1, and v1.2! See the curl_setopt for more information.
For this particular error I ended up using the following code:
$guzzle = new GuzzleClient('https://somesite.com', array(
'curl.options' => array(
'CURLOPT_SSLVERSION' => 'CURL_SSLVERSION_TLSv1_1',
)
));
The site I was trying to connect to would not accept any other option including CURL_SSLVERSION_TLSv1.

Categories