Nɪsʜᴀɴᴛʜ ॐ
Nɪsʜᴀɴᴛʜ ॐ

Reputation: 2914

Aw, Snap! Something went while displaying this webpage

From Cpanel Live Server, I have written an API in PHP to fetch all the products and related stuffs.
Out of 4999 products only 894 products are saving taking 1.7 seconds. Altogether 15 tables with related data are dumping into Database.

After 1.7 seconds, I'm getting as

Aw, Snap! Something went while displaying this webpage in chrome

I thought it was to clear browser cache with php.

So I tried to Reset from client-side of the chrome from the following way described below:

  1. Unchecked Use hardware acceleration when available
  2. Hard Refresh or Reloading Ctrl+F5 to reload the site by bypassing the local cache doesn't solve the problem
  3. Cleared Chrome Browsing cookies Data and cache
  4. Removed all Chrome Extensions
  5. Tried to launch the browser by the command: /usr/bin/google-chrome-stable %U --ignore-certificat --allow-no-sandbox-job --disable-gpu --ssl-version-min=tls1 --disable-print-previewe-errors
  6. clear “Cached images and files”

The above method didn't worked

On Inspecting from Chrome DevTools

General

Request URL: https://www.SomeDomain.in/index.php?route=api/synchronization/checkapikey
Request Method: POST
Status Code: 200 OK
Remote Address: 56.212.130.100:443
Referrer Policy: no-referrer-when-downgrade

Response Headers

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With
Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS
Access-Control-Allow-Origin: https://www.SomeOtherDomain.com
Access-Control-Max-Age: 1000
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection: Keep-Alive
Content-Type: application/json
Date: Tue, 27 Mar 2018 09:20:03 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive: timeout=2, max=500
Pragma: no-cache
Server: Apache
Set-Cookie: currency=INR; expires=Thu, 26-Apr-2018 09:20:03 GMT; Max-Age=2592000; path=/; domain=www.SomeDomain.in
Set-Cookie: language=en-gb; expires=Thu, 26-Apr-2018 09:20:03 GMT; Max-Age=2592000; path=/; domain=www.SomeDomain.in
Set-Cookie: PHPSESSID=73eiaild74dllqv2qu29acl964; path=/; HttpOnly
Set-Cookie: default=8cipi68v54vf2l6mot7tofi923; path=/; httponly
Transfer-Encoding: chunked

Request Headers

Accept: */ *
Accept-Encoding: gzip, deflate, br
Accept-Language: en,en-GB;q=0.9,en-US;q=0.8
AlexaToolbar-ALX_NS_PH: AlexaToolbar/alx-4.0.1
Connection: keep-alive
Content-Length: 24
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
DNT: 1
Host: www.SomeDomain.in
Origin: https://www.SomeOtherDomain.com
Referer: https://www.SomeOtherDomain.com/admin/index.php?route=account/apisync&token=9Yzmh4XTEjFB4p2Jk5jaMJOlm78Z9nzt
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36

After adding PHP code below in the controller

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Expires: 0");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-Type: application/xml; charset=utf-8");

But one thing worked was Cached images and files: 120 MB. After adding the above code it was set to 2.5 MB at the end of the result


Results from the Response Headers:

Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0
Connection: Keep-Alive
Content-Encoding: gzip
Content-Type: application/xml; charset=utf-8
Date: Tue, 27 Mar 2018 09:21:31 GMT
Expires: 0
Keep-Alive: timeout=2, max=500
Last-Modified: Tue, 27 Mar 2018 09:21:54 GMT
Pragma: no-cache
Server: Apache
Set-Cookie: default=ip18higdsc0v55ncmn98f0k346; path=/; httponly
Transfer-Encoding: chunked
Vary: Accept-Encoding

I had also used CURL function to carry out with ease:

$headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';              
$headers[] = 'Connection: Keep-Alive';         
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';         
$user_agent = 'php';         
$process = curl_init();         
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);         
curl_setopt($process, CURLOPT_HEADER, 0);         
curl_setopt($process, CURLOPT_USERAGENT, $user_agent);    
curl_setopt($process, CURLOPT_TIMEOUT, 30);         
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);         
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);         
$return = curl_exec($process);         
curl_close($process); 

As I'am unable to figure out the right solution, for the above cause? What might be the reason for getting such error?

Upvotes: 0

Views: 2472

Answers (1)

Nɪsʜᴀɴᴛʜ ॐ
Nɪsʜᴀɴᴛʜ ॐ

Reputation: 2914

Finally issue was solved by taking 26.1 mins to upload all the 4999 products in one shot with the request transfer size of 20.1 MB, and with all the related 15 tables. Without even configuring Chrome Settings, left it, as it is by default.

Since it took 26.1 mins because of more than 25,000 images needs to be retrieved from the server to server with a limited bandwidth.

Also, Make sure your RAM size should be greater than 4GB

By adding some configuration to the CURL and PHP:

In CURL,

curl_setopt($process, CURLOPT_TIMEOUT, 1800); #Drop connection after 60*30 seconds

# This is inadvisable in a production environment. If the CONNECTTIMEOUT set to Zero
curl_setopt($process, CURLOPT_CONNECTTIMEOUT, 0); # CURLOPT_CONNECTTIMEOUT <= CURLOPT_TIMEOUT in seconds

In PHP, again, you must remove time limits or PHP itself (after 30 seconds by default) will kill the script along Curl's request. This alone should fix your issue.
In addition, if you require data integrity, you could add a layer of security by using ignore_user_abort:

# The maximum execution time, in seconds. If set to zero, no time limit is imposed.
set_time_limit(0);

# Make sure to keep alive the script when a client disconnect.
ignore_user_abort(true);

A client disconnection will interrupt the execution of the script and possibly damaging data,
eg. non-transitional database query, building a config file, ecc., while in your case it would download a partial file... and you might, or not, care about this.

In addition, you are free to try-to-do so,

ob_start(); // start output buffer
ob_end_clean(); // discard output buffer

For Improving performance:
You can cache API output or make API calls in the background. Establish reasonable timeouts for API requests and, if possible, be prepared to display output without an API response. This, in turn, puts the over-head to the client's browser

curl_setopt($curl1, CURLOPT_FRESH_CONNECT, 1); // don't use a cached version of the url 

CURLOPT_FRESH_CONNECT TRUE to force use of a new connection instead of a cached one.

you can set header

$headers = array( 
                 "Cache-Control: no-cache", 
                ); 
curl_setopt($curl1, CURLOPT_HTTPHEADER, $headers);

In PHP,

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);

Updated:

When 500 Internal Server Error encountered a.k.a., White Screen of Death

Cause: PHP will run a 500 error when dealing with any file or folder that has permissions set to 777

Solution: A PHP file must have permissions set to 644. Any folder containing PHP files and PHP access (to upload files, mkdir cmds for example) must have permissions set to 755.

If the above solution doesn't work try to set

error_reporting(E_ALL);
ini_set('display_errors', 'On');
error_reporting(-1); // reports all errors
ini_set("display_errors", "1"); // shows all errors
ini_set("log_errors", 1);
ini_set("error_log", "/tmp/php-error.log");

Always retrieve the JSON encoded data to the controller and it's highly secured rather than retrieving from the view and then passing into a controller that depends upon where does your calling function is placed. This alone may eliminate's the RAM size dependency.

Upvotes: 1

Related Questions