Posts

Showing posts with the label php

Twitter API with PHP

Image
In order to use a communicante with a website ( in this case twitter) , we need to call it's  api But there too many standars to communicate with api ( OAuth1, OAuth2...) , hopefully they provides api documentation . In this tutoriel we are gonna use twitter api ( 1.1  ) as exemple to get ou last tweets , but first we have to create twitter application Creating twitter application give us  some random strings `consumer_secret`, `consumer_key`, `access_token` and  `access_token_secret` We will use these strings later in OAuth process. Don't panic no need to knowing all OAuth process using  php , Moreover, there are many php libraries and packages to do this for us , there is a very popular one ( twitter-api-php ) first we create an empty folder for our project then require the package : composer require  j7mbo/twitter-api-php copy and past random strings from your twitter application in to this array  variable :...

Upload an image with AJAX

Image
To begin, little reminder about sending a form (without upload) AJAX with jQuery. Nothing less than a form for sending in AJAX. Here in this example will be about an upload image, but can be adapted to any other type of file. Here is the code that supports the upload of an image, in addition to the rest of the form. enctype added = "multipart / form-data" to the form is mandatory if you want to do a file upload. We also add our field that allows the user to select a file on his computer. It states that only the images are accepted. The upload AJAX is related in fact to support the browser FormData API. This is supported by the major browsers finaly PHP : You can use my other tutoriel to handle this php file  .

Upload files with Php

Image
When it comes to upload files with php, it's not so difficult, In the beggining we just need to type some  html markup we just creating a form with enctype = " multipart/form-data " it important to add this attribute it shows that we can upload file with this form < input type = " hidden " name = " MAX_FILE_SIZE " value = " 30000 " /> this hidden field indicate max file size , we need also to make security check in php side < input name = " userfile " type = " file " /> this is the file input to upload a file move_uploaded_file ( ) : take 2 paramatres the filename and destination path/file NB : this just uploading directly files ,but in the production mode wee need  security checks such as allowed file extensions and allowed max file size $_FILES it's multidimensional array variable hols all uploaded file info

Realtime Chat Application with CakePHP and Socket.IO #2

Image
Before , We begin we need the main layout for the application ,in my case i  make ctp file (cakephp file ) I include in the css and js files for   Material Design for Bootstrap  just to have a modern look for the application <?php echo $this -> fetch( ' content ' ); ? > this echo the view for other pages / controllers's actions we need also to another layout file  to use it in chat PS : the layout  files are in this directory app/view/layout , it's specific for cakephp 2

Realtime Chat Application with CakePHP and Socket.IO #1

Image
In this blog we will build a Realtime Chat Application with CakePHP and Socket.IO Your are free to use another framework, the idea is just learning the basics and a few (Socket.IO) tips Database :  To store our data for this application we need 4 tables :  users :         contains user's informations such as username and password hash ,email ...  messages : contains messages between users, it also store the information of sender and destination  user companies : or teams table ,in order to organise groups i add this table to give the user the freedom  of organizing his contact it stores the company name with his owner .   companies_users : it contains the users and their respective  companies here the SQL file fo database structure     file in the next tutorial we'r gone started building the frontend using cakephp  

Creating my First PHP Framework : Day 4

Image
After knowing which controller to load with App.php  , we need to know what the view corresponding, or simply create a functions to load the view in Controller.php in this file we find a function called view , it has 3 parameters -  $view : the view file name -  $data = [] : array of a view variables ,if we want send variables to the view -  $template = " default " : The view layout it loads by default , default.php exemple : $this -> view( ' pages/home ' , $data = [ ' name ' => $name ]); the second function model is just requiring the model for the controller require_once " ../app/models/ " . $model . " .php " ; and getting instant of it return new $model (); Tip : ob_start   is more like to save the view file content inside  a variable

Creating my First PHP Framework : Day 3

Image
The Controller Ohhh this magic piece of code  ,  to simplify things the role of controller is knowing which part of code to load . The idea here is a creating a class to auto load a model  and a functions to load view(template) , but... wait we have to know witch controller to load first , don't worry the is the Magic App.php file : Each time we load an url we have to decompose with (  URLparser function ) it into BASE_URL/CONTROLLER/ACTION/PARAMS in this file we take the url , extract the controller'name  as array   , after we require the controller file(after checking if exist) require_once " ../app/controllers/ " . $this -> controller . ' .php ' ; Lastly , we use  call_user_func_array to call the URL call_user_func_array ( [ $this -> controller , $this -> method ], $this -> params ); The framework github

Creating my First PHP Framework : Day 2

Image
First things first , i'needed a file (init.php) witch requires all other components of the framework , also all the static variables such as APP and CSS directories so i called it init.php                                                       <?php define ( ' ROOT ' , dirname ( __DIR__ )); define ( ' APP ' , ROOT . "/ app /" ); define ( ' WEBROOT ' , " http:// " . $_SERVER [ ' HTTP_HOST ' ] . dirname ( $_SERVER [ ' PHP_SELF ' ]) . " / " ); define ( ' CSS ' , WEBROOT . " css/ " ); define ( ' IMG ' , WEBROOT . " img/ " ); define ( ' JS ' , WEBROOT . " js/ " ); define ( ' DS ' , ROOT . " / " ); require_once ROOT . ' /vendor/autoload.php ' ; require_once APP . ' /core/App.php ' ; require_once APP . ' /database.php ' ; Also i need to require 3 files autoload.php : composer's a...

Creating my First PHP Framework : Day 1

Image
Why ?  So why should you write your own framework? Firstly, it is a great learning experience. You will get to learn PHP inside-out. You will get to learn object-oriented programming, design patterns and considerations. More importantly, you have complete control over your framework. Your ideas can be built right into your framework. Although always not beneficial, you can write coding conventions/functions/modules the way you like. 11 Months ago i started building MVC Php framework ,   the idea was create a simple flixible framework   which any developer can use it without problem ORM The ORM  is a kind of abstraction of database , it avoids you to to write long SQL statmemnts using  functions eg save to INSERT data or to find   to SELECT data use Cake\ORM\TableRegistry ; $data = [ ' title ' => ' My first article ' , ' body ' => ' It is a great article ' , ' user_id ' => 1 , ...