Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 ALL


  PHP to get access token for Sina Weibo app

Previously I wrote two articles about getting access token for Facebook and Twitter apps using PHP. Today I will write one more article about getting access token for Sina Weibo app using PHP.OAuth 2.0 is now the authorization mechanism of Sina Weibo API. The API authorization process is similar to the process of Twitter. It has basically two steps: 1. Authorization; 2. Get access token.1. Create an app.I hope you know how to create an app in Sina Weibo now. If not. You can access this page and it will guide you on how to create the app. After you creating the app. You should write down the Ap...

18,486 2       PHP ACCESS TOKEN SINA WEIBO


  PHP advisory file lock : flock

When we process a file in PHP, we may often need to acquire a lock so that other scripts cannot edit the same file at the same time. There is a flock() function in PHP which can help us lock the file we want to process. But there is one issue we should take care.Recently, ffb encountered one issue while he was trying to lock a file handle. The codes are below:$filename = "/tmp/lock.txt";    $fp = fopen($filename, "r+");  if (!$fp) {      die("open failed.");  }    i...

8,153 0       PHP FLOCK() ADVISORY LOCKING


  PHP to get access token for Twitter app

Previously we wrote an article about getting access token for Facebook app--PHP to get access token for Facebook app. Today we will introduce how to get access token for Twitter app using PHP.Since now Twitter is also using OAuth 2.0 to allow some web apps to access some users information on behalf of one user. They provided some APIs for developers to easily get them integrated with their own websites. The first step to get all these done is how to get the access token, the access token seems like the password to one user's account on Twitter.First, we need to download the SDK, in this articl...

16,063 0       PHP TWITTER OAUTH ACCESS TOKEN


  Output a file with HTTP range header in PHP

When downloading a large file, we may encounter some network issues which causes download termination and only part of the file is downloaded. When the network connection resumes next time, we may need to redownload the file again from the beginning. In order to save bandwidth, http provides a Range parameter in its header which can control the file transfer flow. With the range parameter in the header, we can resume the download from where we stop.Here is a piece of PHP code snippet which uses the range header to control which part of the file to transfer: <?php $filename=$_GET['fi...

29,284 3       PHP HTTP RANGE FILE TRANSFER


  How does PHP session work?

This article is about how PHP session works internally. Below are the steps :1. Session in PHP is loaded into PHP core as an extension, we can understand it as an extension. When session extension is loaded, PHP will call core functions to get the session save_handler, i.e interface or functions for reading and writing session data. By default, PHP will handle session data by writing and reading files on the server. But PHP also supplies custom methods for handling session data, we can use session_set_save_handler() to register the save_handler. At the same time, PHP will check whether session...

10,256 0       PHP SESSION MECHANISM


  File upload in PHP

File is a special kind of form data, when being uploaded to the server through HTTP POST request, PHP will create a $_FILES global array, the relevant file information will be stored in this global array. We will illustrate file upload with some code snippets using PHP and look into the internal work mechanism. Lastly will talk about file upload security.File uploadIn order for users to upload files in client side, we have to provide a form on the user interface. Since the uploaded file is a special data, it is different from other POST data, so we have have to set a special encoding for the f...

4,482 1       PHP FILE UPLOAD


  Tips for improving PHP efficiency

0. Using single quote to replace double quote to enclose string literal, this will be a bit faster. Because PHP engine will search variables in double quoted string.1. If a method in class can be declared as static, then make it static, this will be 4 times faster.2. $row["id"] is 7 times faster than $row[id]3. echo is faster than print, and you should use multiple parameters instead of string concatenation, i.e use comma(,) instead of dot(.) to concatenate string. For example echo $str1,$str24. Calculate the numer of loops before executing for loop, don;t calculate the number of loops in each...

14,913 0       PHP EFFICIENCY TIPS


  One reason why mcrypt responds slowly

This morning one colleague came over and talked about one script which used mcrypt responded very slowly, the server configurations are fine. But the reason for the slowness is unknown.Here is one script which reproduces the issue:<?php$dmcryptText = "dummy";$key = "foobar";$size = mcrypt_get_iv_size(MCRYPT_BLOWFISH,MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($size); //Take care $m = mcrypt_ecb(MCRYPT_BLOWFISH, $key, $dmcryptText, MCRYPT_DECRYPT, $iv);var_dump($m);When 20 requests of this script are sent to the server in parallel, the response time of Apache server increases rapidly.The r...

9,121 8       PHP SOLUTION SLOW MCRYPT RESPONSE