Redirect http url to https

Force redirect http to https using htaccess

Force redirect http traffic to https

Redirect http to https url using simple file. If you have recently switched to https version, which is recommended, then you want to force the traffic to https.

This can be accomplished using .htaccess file.

How to redirect https to http

1. Create/update the .htaccess file on your root website.
2. Add the following line on the top part of the .htaccess file


RewriteEngine On

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

3. That is it. You don’t need to restart the server for this case.

You can refer this page for more htaccess examples

Apache Not Service Files from Icons Folder

My ico, png, jpg and more files from Apache are not being served

It is a bit crazy but the icons folder that is residing in the root folder is reserved. Well kind of.

If you have icons folder in the root, then your files under that folder might have hard time being served.

What is the fix

The easiest is to rename your “icons” folder. If you have admin access to the apache config files, on one of the conf files, search for
Alias /icons/ .. and change it accordingly.

facebook social marketing

Add batch/multiple posts to Facebook Page – Graph API

facebook graph API batch request usage

If you have or managing any facebook page, chances are you might be using faebook’s graph API. It comes with lots of flavors of languages like Java, PHP, Javascript.

Ok you know about that, this post is not about that. If you want to see how to make a post to facebook through graph API, look at this post https://gullele.com/using-facebook-api-post-articles-php/

This post is about making multiple posts to Facebook page.

Why would I need batch requests

Imagine you have 20 blog articles you want to post to facebook. If you are not using batch, then it means you are sending 20 post requests to facebook. And as you can imaging it is easy to hit the limit easily. Facebook will stop you from bombarding their site.

When you are using batch, you have only one request, holding all 20 posts. Yup, that is it. One request vs 20. Now you know why you have to use that.

How to send multiple blog posts or other posts to Facebook page at once

If you follow the above link I referenced, you will have an idea on how single post is doing. I will just continue on that example class and will add just one more method to handle batch request.

The main idea is, each blog or whatever post has to be an object of Request. And you will send all those requests wrapped in one and send it.


	/**
	 * Send batch request to FB than a single one
	 * @param $post_data
	 */
	private function batchPost(array $post_data, $token)
	{
		/*
		 * In the case of the batch request, each has to be an object of request. And all those can be 
		 * send as one big request.
		 */
		$batch_response = [];
		$requests = [];
		foreach ($post_data as $id => $data) {
			$requests[$id] = $this->facebook->request('POST', '/me/feed', $data);
		}

		$batch_response = $this->facebook->sendBatchRequest($requests, $token);

		return $batch_response;
	}

In the above example, the method will be accepting $post_data as a parameter. If a post example is considered, a single $data will look like


$data = [
			'link' => "https://gullele.com/link-goes-here",
			'message'=>"how to make batch request in facebook api",
			'title'=>"Making Multiple Requests in Facebook",
			'description' => 'In this article I will show you how you can make multiple blog posts to Facebook page with one single request.',
		];

Then the line with $this->facebook->request will create a request object and add it as part of request array. Also mind the $id, each should have unique key in the $request array.

Then Facebook will send you back the response with post id and other lots of information that you might want to keep it for later reference like to get metrics on the post – how many likes, impressions, shares.. which will help you to improve in the long run.

Let me know if you have any questions or other methods.

facebook social marketing

Post to Facebook From app – Using PHP

Post to Facebook programatically

You can post whatever you like from PHP to your or company’s page. Facebook has provided an SDK that makes life easier to post to the Facebook page.

This is ideal if you want to post to facebook, from cron job especially. Or on some event like right after the post in wordpress.

First thing first, you need to have a developer account on Facebook and you can do that easily and free on https://developers.facebook.com

Then you need to create an app. A single page or something like that would work. You will see how to create one when you are setting up your account for developer.

Once you have that, on the left menu, you will see dashboard. This contains important information about the app you will use to interact from your code.
Continue reading Post to Facebook From app – Using PHP

sftp connection

connect to sftp from php

connecting sftp using php

What is SFTP
When to use SFTP
Automating SFTP
What to consider when automating
SFTP download using php code
SFTP upload using php code
List all files on SFTP Server

What is SFTP

SFTP, Secure File Transfer Protocol, is more applicable these days where cyber security is at its peak. Whenever possible, one has to use SFTP as the communications are encrypted.

Most of tools that are used for FTP like filezilla allows using SFTP with minor adjustment. SFTP has to be supported by the server that is providing the FTP server and usually uses different port than that of the FTP.

SFTP is pretty much like FTP as far the connection and usage is concerned. It has just another layer of security to make it more secure while uploading and downloading the file.

Basic server address, username, password and port are still necessary to connect and interact with SFTP server.
Continue reading connect to sftp from php

fos user bundle security validation

A login authentication would be handled by securitybundle in symfony.

But how about if you need to validate or authenticate username and password from controller?

Here is what you can do

        
        public function authenticatememberAction($user, $password)
        {
           $valid = false;

           $user_manager = $this->get('fos_user.user_manager');
           $factory = $this->get('security.encoder_factory');

           $user = $user_manager->findUserByUsername($user);//get the user first
           if (!empty($user)) {
              $encoder = $factory->getEncoder($user); //then the encoder
              $valid = ($encoder->isPasswordValid($user->getPassword(),$password,$user->getSalt())) ? true : false;
           }

           return new JsonResponse(['valid'=>$valid]);
        }

The above snippet will validate if the provided username password combination is correct or not.

http post

$_POST vs $HTTP_RAW_POST_DATA vs php://input and enctype

This post will answer the following questions:

  • What is form HTTP Request POST ?
  • What is enctype?
  • How to access the posted values?
  • Why is the $_POST not having any data in it?

What is HTTP Request POST?

Continue reading $_POST vs $HTTP_RAW_POST_DATA vs php://input and enctype

taking off of the airplane

how to deploy symfony application to the production server

Deploy Symfony application to production – setting it live

This is a continuation of deploying symfony application to the server part one

Deploy Symfony application to production part two

4. Installing composer dependencies

Be in your symfony app first

cd /apps/symfony-app

The run the installer here

composer install --no-dev --optimize-autoloader

Under most circumstances, this will run without any problem. If there are any problems, composer will log them for you so follow those and solve those. And this will create a vendor folder in the folder you run composer. Continue reading how to deploy symfony application to the production server

airplane in he runway

Deploying Symfony on Production Server

Time to ship your symfony app to production? Here are some flight checks. Lets assume your symfony app is in folder symfony-app

1. Don’t put Symfony app in the public accessible folder on your server

If your servers public facing folder is, say, /var/www/html, then don’t put the whole symfony folder there.

Choose another deeper and non-public facing folder. Let say /apps/symfony-app. I will list how you would put the public facing folders later.
For this to happen run this command on your server

mkdir /apps

2. Pushing your code to production server

You can use FTP client like filezilla or cyberduck and put your code in /apps
If you are using git, then you can clone your app like this

cd /apps
git clone https://username@bitbucket.org/username/symfony-app.git symfony-app

Continue reading Deploying Symfony on Production Server

test

Run single phpunit test

Well, we all love php unit tests :). There are times we would like to run one/single test from test suite that has failed so that we can fix or look at it thoroughly. PHPUnit has a way to do it.
The problem, when you are your php unit suite as

phpunit -c /testFolder..

All you php tests would run.
The solution for this is:
Let’s assume you have this particular php unit test in one of your test files

public function testPhpIsAwesomeOrNot(){
}

All you have to do is to add the <b>group annotation</b> above the function of your php unit test

/**
 * @group awesome
 */
public function testPhpIsAwesomeOrNot(){}

and run your PHPUnit single test wiht –group annotation

phpunit -c /testfolder --group awesome

The above would run single PHPUnit test from the test suite.