cURL vs “file_get_contents()” in Google App Engine – Performance analysis

We all know that to do any kinds of external HTTP request from your PHP application (deployed in Google App Engine a.k.a GAE) we have three options basically. PHP native cURL extension, “cURL Lite” provided by Google and the native http:// and https:// PHP stream handlers.

cURL requires a valid billing profile and only you can enable it in your Google Cloud paid project. And that’s why Google’s custom cURL Lite actually use Google’s urlfetch service that you can use in your application free version.

But recent days, our engineering team was just wondering which can be little bit faster among cURL or cURL Lite or PHP native PHP HTTP handler, in this sense little bit faster meaning we also count even 50ms latency. That’s why I was running some test with a single script hosted on Google App Engine (PHP Standard Runtime environment). We had lots of PHP microservice apps hosted on Google App Engine and all services at a certain time needs to talk each other via HTTP external request. But sometimes, we were aware that latency is killing some communication.

So we just built 2 files basically in PHP. One is using cURL to post some foobar json data to an external URL (postb.in) and another one was using the native http:// and https:// PHP stream handlers. Let’s see what was our experimental scripts look like.

<?php

/**
 * Using CURL
 */

$data = array("foo" => "bar");
$data_string = json_encode($data);

$ch = curl_init('http://postb.in/xxxxxx');
//$ch = curl_init('/post.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string)
    )
);

$result = curl_exec($ch);
<?php

/**
 * php_http_stream.php
 *
 * using cURL lite. It use google's urlfetch service
 */

$url = 'http://postb.in/xxxxxx';
$data = array("foo" => "bar");
$data_string = json_encode($data);

$contentLength = strlen($data_string);

$headers = "accept: */*\r\n" .
    "Content-Type: application/json\r\n" .
    "Content-Length: $contentLength\r\n";

$context = [
    'http' => [
        'method' => 'POST',
        'header' => $headers,
        'content' => $data_string,
    ]
];
$context = stream_context_create($context);
$result = file_get_contents($url, false, $context);

And here is the trace report of those two call.

@0 ms
/php_http_stream.php
Summary
Name			RPCs	Total Duration (ms)
/curl_lite.php		1	450
/urlfetch.Fetch		1	333
Details
Timestamp		xxxx-xx-xx (xx:xx:xx.xxx)
Traced time 		333 ms
Untraced time 		117 ms

http/response/size	25
@0 ms
/curl.php
Summary
Name				RPCs	Total Duration (ms)
/curl.php			1	753
/remote_socket.Close		4	4
/remote_socket.Connect		10	157
/remote_socket.CreateSocket	4	10
/remote_socket.GetSocketOptions	1	1
/remote_socket.Poll		10	469
/remote_socket.Receive		2	2
/remote_socket.Send		2	2
/remote_socket.SetSocketOptions	1	1
Details
Timestamp	2017-xx-xx (xx:xx:xx.xxx)
Traced time 	646 ms
Untraced time 	107 ms

http/response/size		25

So what does it mean to you? It means a lot for me. Obviously cURL Lite is saving me couple milliseconds. And also I don’t need to be afraid of my “socket operation” quota that was used in cURL.

So in this, what should I say? file_get_contents() is more optimized? Of course, I am just talking how it’s performing for little external URL call with Google’s urlfetch service.

So if your application needs to interact with external service with less configuration and options, then I would prefer to use native PHP HTTP stream handler and make all external http call with file_get_contents() function. file_get_contents() use urlfetch service and you don’t need to enable cURL extension in your application.

Changing File Permissions in cPanel

Permissions can be very important when it comes to hosting your website. Permissions can allow our server computer to write and edit your files. Along with that, some files need to be protected from writing and editing, as a security measure. You can change your file permissions in many ways.

Step 1: Open your File Manager and navigate to the file or folder that you need to change.

Step 2: Click on the name of the file or folder.

Step 3: Click on the Change Permissions link in the top menu of the File Manager page.

Step 4: Click on as many check boxes as you require to create the right permission. The permission numbers underneath the check boxes will update automatically.

Step 5: Click on the Change Permissions button when you are ready. The new permission level is saved and the display updated to show the modified file.

Setting up PHP, MySQL, phpmyadmin on mac sierra

Mac OS Sierra comes with PHP, MySQL pre installed isn’t it cool? There are two-label root directory, one is called system label and another is user label. The default system label root is “/Library/WebServer/Documents”. The other user label root is missing by default. In this article we’re going to work with user label root.

PHP installation

Create folder named “Sites” at your root directory. (For those who are newbies) From terminal type “cd” and “sudo mkdir Sites”. To run this user label system make a configuration file under /etc/apache2/users/{your-username}.conf (here in the place of “your-username” type your currently logged in users username) and place the below code in it.

<Directory "/Users/username/Sites/">
AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require all granted
</Directory>

 

permission this file should be “-rw- r– r– 1 root” if not, run this command “sudo chmod 644 {your-username}.conf”

Next open the “/etc/apache2/httpd.conf” file to active some module

✓ LoadModule authz_core_module libexec/apache2/mod_authz_core.so
✓ LoadModule authz_host_module libexec/apache2/mod_authz_host.so
✓ LoadModule userdir_module libexec/apache2/mod_userdir.so
✓ LoadModule include_module libexec/apache2/mod_include.so
✓ LoadModule rewrite_module libexec/apache2/mod_rewrite.so
✓ LoadModule php5_module libexec/apache2/libphp5.so
✓ Include /private/etc/apache2/extra/httpd-userdir.conf

Then open another apache configuration file at “/etc/apache2/extra/httpd-userdir.conf “ and active module

✓ Include /private/etc/apache2/users/*.conf

Save and restart apache “sudo apachectl restart”

If your are going the web serving document root at “/Library/WebServer/Documents”, its good idea to allow any .htaccess file used to override the default settings. This can be done by editing the “/etc/apache2/httpd.conf” file “AllowOverride All”. Also uncomment the below module

✓ LoadModule rewrite_module libexec/apache2/mod_rewrite.so

To test php running write a php file under /Library/WebServer/Documents and name it according your wish and run through browser “localhost/{your-file- name.php}”

MySQL installation

Download MySQL from MySQL’s native website and install it manually.
The first time after installations a dialog box will popup showing a temporary password, save that carefully for future use. Stop MySQL and start in safe mood and change the given password to yours by running these below commands.

✓ sudo /usr/local/mysql/support-files/mysql.server stop
✓ sudo mysqld_safe — skip-grant- tables
✓ mysql -u root
✓ FLUSH PRIVILEGES;
✓ ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘MyNewPass’;
✓ sudo /usr/local/mysql/support-files/mysql.server start

You may be encountering the problem/error “ERROR! MySQL server PID file could not be found!”.

The problem is because the MySQL pid file isn’t in the MySQL configuration file. The default configuration sample file directory is “/usr/local/mysql/support-files/my-default.cnf”

Create a MySQL configuration file copy and pasting from “my-default.cnf tomy.cnf” And the PID file is under the directory “/usr/local/mysql/data/{your-pc-name}.pid”. At the bottom of configuration file write the MySQL pid path like “pid-file = /usr/local/var/mysql/{username}.pid”. look for running MySQL using command “ps -A | grep MySQL” and kill it “sudo kill -15 pidno” if any.
start MySQL “/usr/local/mysql/bin/mysql -v -uroot -p” here you will be promoted for password. Type the temporary password given/shown. First change the password “ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘MyNewPass’;”

To work with MySQL through terminal “/usr/local/mysql/bin/mysql -u root –p”

PHPMYADMIN

Download phpmyadmin, from your download directory rename it phpmyadmin and put it under (you created earlier) Sites folder Make a directory under phpmyadmin and name it config “Sudo mkdir ~/Sites/phpmyadmin/config”

Change the permission “chmod o+w ~/Sites/phpmyadmin/config”

Browse http://localhost/~{your-username}/phpmyadmin/setup/

Click on “new server” and then on Authentication tab fill the “password for config auth” with your desired password. Click save and then save. move the file under the config directory to direct phpmyadmin folder (one level up) and remove the config directory.

Browse http://localhost/~username/phpmyadmin/

Adding Custom Validation Rules in Valitron

Valitron is a simple, minimal and elegant stand-alone validation library with NO dependencies. Valitron uses simple, straightforward validation methods with a focus on readable and concise syntax. Valitron is the simple and pragmatic validation library we’ve been looking for.

Adding Custom Validation Rules :

To add your own validation rule, use the addInstanceRule method with a rule name, a custom callback or closure, and a error message to display in case of an error. The callback provided should return boolean true or false.

such as :

$validation->addInstanceRule(‘functionName’, function () {
return false;
});

$validation->rule(‘functionName’, ‘fieldName’)->message(‘Error Message’);

 

We can also use the following structure :

if (condition) {

$validation->addInstanceRule(‘functionName’, function () {
return false;
});

$validation->rule(‘functionName’, ‘fieldName’)->message(‘Error Message’);

}

 

 

Android – Get current location of user by using LocationManager API

Sometimes we need to get user current location from Android device. Its very simple while we use LocationManager API of Android. We just need a custom location manager and a location listener with  necessary permission in Manifest.xml file.  lets see how to do it.

Step-1: Follow the code in manifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.previewtechs.bornolipi">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-feature android:name="android.hardware.location.gps" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme.NoActionBar">
        <activity android:name=".MainActivity" />

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

  
        <activity android:name=".HomeActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
</manifest>

Step-2: Follow these code for TestLocationManager.Java

public class TestLocationManager {

private int minDistanceForUpdateLocation;
private int minTimeForUpdateLocastion;
LocationManager locationManager;
TestLocationListener locationListener;
Context ctx;
private boolean isGPSProviderEnable, isNetworkProviderEnable;

//COnstructor
public TestLocationManager(Context ctx){
this.ctx = ctx;
}

public TestLocationManager(Context ctx, int minTimeForUpdateLocastion, int minDistanceForUpdateLocation ){
this.minDistanceForUpdateLocation =minDistanceForUpdateLocation;
this.minTimeForUpdateLocastion= minTimeForUpdateLocastion;
this.ctx = ctx;
}

//Set Location Manager and Listener
public void setLocationManagerAndListener(){
this.locationListener = new TestSendLocationListener(this.ctx);
this.locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);

isGPSProviderEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkProviderEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if(isGPSProviderEnable){
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTimeForUpdateLocastion, minDistanceForUpdateLocation, locationListener);
}else if(isNetworkProviderEnable){
this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, minTimeForUpdateLocastion, minDistanceForUpdateLocation, locationListener);
}

}

//Get Minimum Distance for updating location
public int getMinDistanceForUpdateLocation() {
return minDistanceForUpdateLocation;
}
//Set Minimum Distance for updating location
public void setMinDistanceForUpdateLocation(int minDistanceForUpdateLocation) {
this.minDistanceForUpdateLocation = minDistanceForUpdateLocation;
}

//Get Minimum Time for updating location
public int getMinTimeForUpdateLocastion() {
return minTimeForUpdateLocastion;
}

//Set Minimum Time for updating location
public void setMinTimeForUpdateLocastion(int minTimeForUpdateLocastion) {
this.minTimeForUpdateLocastion = minTimeForUpdateLocastion;
}
}

Step:3- Follow these code for TestLocationListener.java

public class TestLocationListener implements LocationListener {

private double lat, lng, alt, acc, time;
Context ctx;
ConnectivityDetector connectivityDetector;

public TestLocationListener(Context ctx){
this.ctx = ctx;
}

@Override
public void onLocationChanged(Location location) {
this.lat = location.getLatitude();
this.lng = location.getLongitude();
this.alt = location.getAltitude();
this.acc = location.getAccuracy();
this.time = location.getTime();

Toast.makeText(ctx, “Lat : “+this.lat+” \n Lng : “+this.lng+” \nAlt : “+this.alt+” \n Acc : “+this.acc+” \n Time : “+this.time, Toast.LENGTH_LONG).show();

//to whatever you want with location

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
}

Step:4 – There is the code for MainActivity.java . where we will implement our TesetLocationManager and TestLocationListener .

public class MainActivity extends AppCompatActivity{

    private Toolbar toolbar;
    private Location mLastLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

 // setup toolbar
 toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);


 navigationView = (NavigationView) findViewById(R.id.menu_main);
 navigationView.setNavigationItemSelectedListener(this);
 //Set default item selected on nivigation
 navigationView.getMenu().performIdentifierAction(R.id.menu_home, 0);
 setTitle("");

 //get real time permission for location
 getPermissionForLocation();
 }

 protected void onStart() {
 super.onStart();
 }

 protected void onStop() {
 super.onStop();
 }

 @Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
 switch (requestCode) {
 case ConstantCollection.PERMISSION_REQUEST_LOCATION_COARSE: {
 // If request is cancelled, the result arrays are empty.
 if (grantResults.length > 0
 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
 Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_LONG).show();

 //setup location manager and listener
 setupLocationManagerAndListener();

 //get last known location
 //mLastLocation = getUserLocation();
 //storeUserLocation(mLastLocation);


 } else {
 MainActivity.this.finish();
 Toast.makeText(getApplicationContext(), "Permission denied! You must have to allow this permission", Toast.LENGTH_LONG).show();
 }
 return;
 }
 }
 }


 //Check Permission and get call log list
 private void getPermissionForLocation() {
 // Here, thisActivity is the current activity
 if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)
 != PackageManager.PERMISSION_GRANTED
 && ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
 != PackageManager.PERMISSION_GRANTED) {

 // Should we show an explanation?
 if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
 Manifest.permission.ACCESS_COARSE_LOCATION) ||
 ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
 Manifest.permission.ACCESS_FINE_LOCATION)) {

 // Show an expanation to the user *asynchronously* -- don't block
 // this thread waiting for the user's response! After the user
 // sees the explanation, try again to request the permission.

 ActivityCompat.requestPermissions(MainActivity.this,
 new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
 ConstantCollection.PERMISSION_REQUEST_LOCATION_COARSE);

 } else {
 // No explanation needed, we can request the permission.
 ActivityCompat.requestPermissions(MainActivity.this,
 new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
 ConstantCollection.PERMISSION_REQUEST_LOCATION_COARSE);

 }
 } else {
 //Check permission for location
 setupLocationManagerAndListener();
 
 }
 }

 //setup location location manager and listener
 private void setupLocationManagerAndListener(){
 TestLocationManager locationManager = new TestLocationManager(MainActivity.this, 10000, 10);
 TestLocationListener locationListener = new TestLocationListener(getApplicationContext());
 locationManager.setLocationManagerAndListener();
 }

}

We can find our expected location on onLocationChanged() method of TestLocationListener.java file. So i think you got your expected result. If you have any question then put it on comment box. Thanks.