PHP API call. file_get_contents(…) request failed to open stream: HTTP request failed!

Issue:

When making an API call with

$json = file_get_contents($jsonurl);

You get the following error message:

file_get_contents(your.api.call) request failed to open stream: HTTP request failed!

Description:  In my case the above function works perfectly well when the php file is called from a web browser but return most of the time the aforementioned  error message when run through cron.

Solution:

Use curl to call the api.

$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,$jsonurl);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'YOUR APP NAME');
$json = curl_exec($curl_handle);
curl_close($curl_handle);

Source: https://stackoverflow.com/questions/697472/php-file-get-contents-returns-failed-to-open-stream-http-request-failed

 

Encode special character from php to mysql

Special characters may not be displayed correctly in your mysql data base when you pass php string to mysql statement.

https://github.com/phpmyadmin/phpmyadmin/wiki/Garbled_data

First thing you should do is to make sure that your mysql database encoding is set to utf-8. You can easily do that through phpmyadmin.

Then make sure the string you send are properly encoded in UTF-8 by using the php function http://php.net/manual/en/function.mb-convert-encoding.php

string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding = mb_internal_encoding() ] )

Example:

$name=mb_convert_encoding($response,"UTF-8");

Lastly make sure that the first query you send to you databse is SET NAMES utf8  like in the example below:

$con=mysqli_connect("localhost","my_user","my_password","my_db");
$con->query("SET NAMES utf8");

Remove emojis script and style from wordpress

Dequeue emojis related script in your theme or child-theme function.php by adding this few lines of code:

remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); 
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); 
remove_action( 'wp_print_styles', 'print_emoji_styles' ); 
remove_action( 'admin_print_styles', 'print_emoji_styles' );

What are the http call to wp-cron.php in wordpress server logs?

The apache log of your wordpress server shows:

"POST /wp-cron.php?doing_wp_cron=1542232954.9134418964385986328125 HTTP/1.1" 200 3305 "https://www.example.com/wp-cron.php?doing_wp_cron=1542232954.9134418964385986328125" "WordPress/4.9.8;

WordPress make a http call to wp-cron.php at every site visit. This php script takes care for update check up and for schedule article publication.

WP-Cron works by: on every page load, a list of scheduled tasks is checked to see what needs to be run. Any tasks scheduled to be run will be run during that page load. WP-Cron does not run constantly as the system cron does; it is only triggered on page load.

Source: https://developer.wordpress.org/plugins/cron/

If you want to make sure some tasks are done at a certain time for sure, you can setup a cron task on your server and disable the call to wp-cron.php in the wp-config file.

There is an excellent article from wordpress developper site explaining all the details.

Note: Some sites report that if you use cloudflare or caching plugins, cron may “never run” and therefore recommend the afore-mentioned method. This assertion needs to be verified. Conclusion will be added here.

Hide wordpress error messages and warning

If error messages and warning are essential during development. They are not welcome in your production environment.

We will have a look on how to hide warnings and error messages from the public while keeping this information accessible to the admin (see https://codex.wordpress.org/Editing_wp-config.php#Debug ) .

Method one: wp-config.php  and php.ini

First we will have a look at the wp-config.php file. For this you will need an ftp access to your site.

https://codex.wordpress.org/Editing_wp-config.php#Debug

You will also need access to php.ini

Method 2: wp-config.php only

Replace:

define('WP_DEBUG', false);

by:
ini_set('log_errors','On'); ini_set('display_errors','Off');
ini_set('error_reporting', E_ALL );
define('WP_DEBUG', false);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
Source: https://www.templatemonster.com/help/wordpress-how-to-hide-php-warnings-and-notices.html

With this method nothing is logged (at least on my server).

For a debug friendly version use Method 3:

 // Enable WP_DEBUG mode
define( 'WP_DEBUG', true );

// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', true );

// Disable display of errors and warnings 
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );

// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
define( 'SCRIPT_DEBUG', true );

Source: https://codex.wordpress.org/Debugging_in_WordPress

Message are off and error are logged. The draw back of this methods is that the logs are public at /wp-content/debug.log

How to run phpinfo()

In order to run phpinfo(), create a phpinfo.php file on your server.

You will need to have ftp access to your server.

create a file call phpinfo.php that contains the following code:

<?php
phpinfo()
?>

To run the code, simply open the page you created in your web browser by entering its address in the navigation bar following the example below:

https://example.com/phpinfo.php

given that the file phpinfo.php is at the root of the server. Replace https://example.com/ by your own server address or URL.

You should see a page displayed similar as the one below:

 

W3 Total Cache plugin doesn’t detect apache modules

Issue description: “After running a compatibility check in W3 total cache on WordPress, the following apache modules are not detected:

mod_deflate: Not detected (required for disk enhanced Page Cache and Browser Cache)
mod_env: Not detected (required forhanced Page Cache and Browser Cache)
mod_expires: Not detected (required for disk enhanced Page Cache and Browser Cache)
mod_filter: Not detected (required for disk enhanced Page Cache and Browser Cache)
mod_ext_filter: Not detected (required for disk enhanced Page Cache and Browser Cache)
mod_headers: Not detected (required for disk enhanced Page Cache and Browser Cache)
mod_mime: Not detected (required for disk enhanced Page Cache and Browser Cache)
mod_rewrite: Not detected (required for disk enhanced Page Cache and Browser Cache)
mod_setenvif: Not detected (required for disk enhanced Page Cache and Browser Cache)

source: https://support.plesk.com/hc/en-us/articles/115005108854-W3-Total-Cache-plugin-shows-apache-modules-as-not-detected-

According to this source W3 Total Cache cannot detect php module if mod_php is disabled on an apache server. They also indicate that “mod_php is disabled […] by default because it is not secure (mod_php is outdated).

Solution:

1- Verify that indeed this services are correctly activated.  In order to do so run phpinfo() (see “how to run phpinfo()” )

2- Once confirmed, the error messages can therefore be ignore.

w3 total cache compatibility check not detected