John Davidson

Why would simple PHP Curl https POST requests take more than 30 seconds to complete, from a Mac machine?

0 comments
Message:


I am coding an API in PHP 7.2 to interface with a payment platform. I am doing that on a Mac (Catalina), and my project is reverse proxied by Nginx (Laravel Valet) on the Mac.


The simplest Curl requests take forever to complete. Like, 36 seconds (for only the curl_exec() function to execute), when the SAME request from the same machine takes 1.7 seconds with POSTMAN.


Any idea why that would be?


The Curl report seems fine (sanitised):


*   Trying xxx.0.xxx.78:443...
* Connected to api.paymentplatform.com (xxx.0.xxx.78) port 443 (#0)
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /usr/local/etc/[email protected]/cert.pem
CApath: /usr/local/etc/[email protected]/certs
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server did not agree to a protocol
* Server certificate:
* subject: C=US; ST=California; L=San Jose; O=Destination, Inc.; OU=Destination Production; CN=api.sandbox.paypal.com
* start date: Jul 27 00:00:00 2020 GMT
* expire date: Aug 1 12:00:00 2022 GMT
* issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert SHA2 High Assurance Server CA
* SSL certificate verify ok.
* Server auth using Basic with user 'AfqL2fMLLxXhm6ZNQRtQmqGJKReMEL5jXJMVO4uqpadvIfx6YEAccIxxxxxxxxx-xxxxxxxxxxx'
> POST /v1/oauth2/token HTTP/1.1
Host: api.paymentplatform.com
Authorization: Basic QWZxTDJmTUxMeFhobTZaTlFSdFFtcUdKS1JlTUVMNWpYSk1WTzR1cXBhZHZJZng2WUVBY2NJRm1fY2pob0RZTno2ZS1hV3B3QnhnWU1STlM6RUpSUzNrc1hhSFcybEhIZDAxMVh1Wkt4Y3ZqdXhzYS0wbmlhTWZ6Mzluaxxxxxxxxxxxxxxxxxxxxxxxxx=
Accept-Encoding: deflate, gzip, br, zstd
Accept-Language: en_US
Accept: application/json
Content-Length: 29
Content-Type: application/x-www-form-urlencoded

* upload completely sent off: 29 out of 29 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Cache-Control: max-age=0, no-cache, no-store, must-revalidate
< Content-Length: 701
< Content-Type: application/json
< Date: Fri, 13 Nov 2020 13:45:13 GMT
< Destination-Debug-Id: b6e1e8dda1785
< X-Destination-Token-Service: IAAS
<
* Connection #0 to host api.paymentplatform.com left intact

And the request seems simple enough (and identical to the POSTMAN one):


$curl = curl_init();
$header = array(
'Accept-Language: en_US',
'Accept: application/json'
);
curl_setopt_array($curl, array(
CURLOPT_URL => VB_P['API_URL'].'/v1/oauth2/token',
CURLOPT_RETURNTRANSFER => true,
//CURLOPT_HEADER => 'Accept: application/json',
CURLOPT_HTTPHEADER => $header,
//CURLOPT_HEADER => 'Accept-Language: en_US',
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "grant_type=client_credentials",
CURLOPT_USERPWD => VB_P['CLIENT_ID'].':'.VB_P['CLIENT_SECRET']
));

if (VB_SERV_NAME == 'local') {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
}

$time_start = microtime(true);
$response_json = curl_exec($curl);
$time_end = microtime(true);

Reading some messages on the web, I have tried to add curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);curl_setopt($curl CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
but it doesn't change anything...


Many thanks!
E.



My Answer:
There could be several reasons why your simple PHP Curl HTTPS POST requests are taking more than 30 seconds to complete from a Mac machine. Here are a few possible reasons and solutions:

1. Network latency: The delay could be due to network latency or slow internet connection. Try running the Curl request on a different network or using a VPN to see if the speed improves.

2. DNS resolution: The delay could be caused by slow DNS resolution. Try using IP addresses instead of domain names in your Curl request to see if it speeds up the process.

3. SSL verification: The SSL verification process could be taking longer than expected. Make sure that the SSL certificate is valid and properly configured on the server. You can also try disabling SSL verification temporarily to see if it improves the speed.

4. Server response time: The delay could be due to the server's response time. Check the server's performance and see if there are any bottlenecks that could be causing the delay.

5. Firewall or security settings: The delay could be caused by firewall or security settings on the server or your Mac machine. Make sure that the necessary ports are open and that there are no restrictions that could be slowing down the connection.

6. PHP configuration: Check your PHP configuration settings to ensure that they are optimized for performance. You can also try increasing the timeout value in your Curl request to see if it helps.

Overall, it's important to troubleshoot each possible cause systematically to identify the root of the issue and find a suitable solution.

Rate this post

3 of 5 based on 6479 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us