Thursday, February 10, 2011

Import Photo From Facebook

I was trying to import the photo of the login use of the facebook.I'm putting the step follow those steps and get all the picture of the user.

Step of the import image of the user.

1. Make the Application in facebook (get the api key and secret key)
2. then you need to write following code on the page and upload that page any server.
3. you can click the login page and put the login credential (login name password) after that you can see the all photo of the login user..

code is below there :

require 'facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'API key here ',
'secret' => 'Secret Key here',
'cookie' => true,
));

$session = $facebook->getSession();
$me = null;

if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$token = $session['access_token'];
} catch (FacebookApiException $e) {
error_log($e);
}
}


if ($me) {
$logoutUrl = $facebook->getLogoutUrl();

} else {
$loginUrl = $facebook->getLoginUrl(array('req_perms' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'));

}
?>



$fql_query = array(
'method' => 'fql.query',
'query' => 'SELECT src FROM photo WHERE aid IN ( SELECT aid FROM album WHERE owner='.$uid.' ) ORDER BY created DESC LIMIT 1,42');

$photo = $facebook->api($fql_query);

/*for($i=0; $i{
//echo ''.$me['name'].'';
} */
echo '';
}
else
{
?>
onlogin="Log.info('onlogin callback')">
Login



//echo '';
}
?>





Sunday, April 4, 2010

PHP Code Standard

This is the blog for which are working or making the skill of the programming language in PHP.PHP means Personal Home Page and it is the server side language.

PHP is the server side language and it is making the scope in market for Web application as well as social networking application and desktop application also.

Some points I'm posting in this blog which are very important for PHP.

1. PHP File Formatting
2. Naming Conventions
3. Using Comments in your code.
4. PHP Code Optimization.
5. Coding Style
6. Error and Exceptions
7. Conclusions.

PHP File Format

PHP tag is very simple you can start.

Important you can start like this also but make the change in configuration PHP file
which call php.ini file. then use can use this tag also.

Indentation should consist of 4 spaces. Tabs are not allowed and prevents the accidental injection of trailing white space into the response.

So Now we can use the PHP tag Line termination follows the Unix text file convention.

Classes

Abc standardizes on a class naming convention and whereby the names of the classes directly map to the directories in which they are stored and the root level directory of Abc's standard library is the "Abc/" directory, whereas the root level directory of Abc's extras library is the "Abc/" directory. All Abc classes are stored hierarchically under these root directories...

Naming conventions

Naming conventions make programs more understandable by making them easier to read. They can also givint the information about the function of the identifier-for example, whether it’s a constant, method , package, or class-which can be helpful in understanding the code In a nutshell it is about injecting as much meaning into your code as possible, while still remaining terse and concise. I could just suggest you meditate on the phrase, semantic richness. The readability of your code is important for your benefit and the benefit of your team members. When you follow naming conventions, you increase readability, which increases workflow and enables you to find and fix any errors in your code. All programmers follow a standardized way of writing code; this improves the project in many ways.
Using naming conventions for your variable names can provide you the following benefits:

1. They make your code readable so that you can immediately identify a variable's data type.
1. This can help other developers to understand your code.
12. They are easy to find for and replace when needed.
13. They help reduce conflicts with reserved words and language elements.
14. They can help you distinguish between variables from different scopes (local variables, class properties, parameters, etc.).


Filenames


For all other files, only alphanumeric characters, underscores, and the dash character ("-") are permitted. Spaces are strictly prohibited.
Any file that contains PHP code should end with the extension ".php", with the notable exception of view scripts. The following examples show acceptable filenames for Abc classes:
Abc/Db.php
Abc/Controller/Front.php
Abc/View/Helper/FormRadio.php
File names must map to class names as described above.



PHP Code Optimization

Remember the following guidelines when you optimize your code:
5.1 If a method can be static, declare it static. Speed improvement is by a factor of 4.
5.2 echo is faster than print.
5.3 Use echo's multiple parameters instead of string concatenation.
5.4 Set the maxvalue for your for-loops before and not in the loop.
5.5 Unset your variables to free memory, especially large arrays.
5.6 Avoid magic like __get, __set, __autoload
5.7 require_once() is expensive
5.8 Use full paths in includes and requires, less time spent on resolving the OS paths.
5.9 If you need to find out the time when the script started executing, $_SERVER[’REQUEST_TIME’] is preferred to time()
5.10 See if you can use strncasecmp, strpbrk and stripos instead of regex
5.11 str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4
5.12 If the function, such as string replacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement statements, passing one character at a time, instead of one line of code that accepts arrays as search and replace arguments.
5.13 It's better to use switch statements than multi if, else if, statements.
5.14 Error suppression with @ is very slow.
5.15 Turn on apache's mod_deflate
5.16 Close your database connections when you're done with them
5.17 $row[’id’] is 7 times faster than $row[id]
5.18 Error messages are expensive
5.19 Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time.
5.20 Incrementing a local variable in a method is the fastest. Nearly the same as calling a local variable in a function.
5.21 Incrementing a global variable is 2 times slow than a local var.
5.22 Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable.
5.23 Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one.
5.24 Just declaring a global variable without using it in a function also slows things down (by about the same amount as incrementing a local var). PHP probably does a check to see if the global exists.
5.25 Method invocation appears to be independent of the number of methods defined in the class because I added 10 more methods to the test class (before and after the test method) with no change in performance.
5.26 Methods in derived classes run faster than ones defined in the base class.
5.27 A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar++ operations. A similar method call is of course about 15 $localvar++ operations.
5.28 Surrounding your string by ' instead of " will make things interpret a little faster since php looks for variables inside "..." but not inside '...'. Of course you can only do this when you don't need to have variables in the string.
5.29 When echoing strings it's faster to separate them by comma instead of dot. Note: This only works with echo, which is a function that can take several strings as arguments.
5.30 A PHP script will be served at least 2-10 times slower than a static HTML page by Apache. Try to use more static HTML pages and fewer scripts.
5.31 Your PHP scripts are recompiled every time unless the scripts are cached. Install a PHP caching product to typically increase performance by 25-100% by removing compile times.
5.32 Cache as much as possible. Use memcached - memcached is a high-performance memory object caching system intended to speed up dynamic web applications by alleviating database load. OP code caches are useful so that your script does not have to be compiled on every request
5.32 When working with strings and you need to check that the string is either of a certain length you'd understandably would want to use the strlen() function. This function is pretty quick since it's operation does not perform any calculation but merely return the already known length of a string available in the zval structure (internal C struct used to store variables in PHP). However because strlen() is a function it is still somewhat slow because the function call requires several operations such as lowercase & hashtable lookup followed by the execution of said function. In some instance you can improve the speed of your code by using an isset() trick.
Ex.
PHP Code:
if (strlen($foo) < 5) { echo "Foo is too short"; }

vs.

PHP Code:
if (!isset($foo{5})) { echo "Foo is too short"; }

Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it's execution does not require function lookups and lowercase. This means you have virtually no overhead on top of the actual code that determines the string's length.
5.33 When incrementing or decrementing the value of the variable $i++ happens to be a tad slower then ++$i. This is something PHP specific and does not apply to other languages, so don't go modifying your C or Java code thinking it'll suddenly become faster, it won't. ++$i happens to be faster in PHP because instead of 4 opcodes used for $i++ you only need 3. Post incrementation actually causes in the creation of a temporary var that is then incremented. While pre-incrementation increases the original value directly. This is one of the optimization that opcode optimized like Xyz's PHP optimizer. It is a still a good idea to keep in mind since not all opcode optimizers perform this optimization and there are plenty of ISPs and servers running without an opcode optimizer.
5.34 Not everything has to be OOP, often it is too much overhead, each method and object call consumes a lot of memory.
5.35 Do not implement every data structure as a class, arrays are useful, too
5.36 Don't split methods too much, think, which code you will really re-use
5.37 You can always split the code of a method later, when needed
5.38 Make use of the countless predefined functions
5.39 If you have very time consuming functions in your code, consider writing them as C extensions
5.40 mod_gzip which is available as an Apache module compresses your data on the fly and can reduce the data to transfer up to 80%



Comments

Using comments consistently in your PHP 5.0/ code allows you to describe complex areas of code or important interactions that are not otherwise clear. Comments must clearly explain the intuition of the code and not just translate the code. If something is not readily obvious in the code, add comments to it.
While using Auto Format tool with your code, you will notice that trailing comments move to the next line. After you format your code, you can add these comments, or you must modify the comment's new location after you use the Auto Format tool.
Use the following guidelines when you add comments to your code:
2. Use block comments (/* and */) for multiline comments and single-line comments (//) for short comments.
3. Remove any un-useful comments from the code before you deploy your project. If you find that you have
4. many comments in your PHP code, consider whether you need to rewrite some of it. If you feel you must
5. Include many comments about how the PHP code works; it is usually a sign of poorly written code.

Exception best practices
• Use specific derived exceptions in both throw and catch. See the following two items:
Avoid throwing the Exception base class, or other exception superclass. The more specific the exception, the better it communicates to the user what happened.
Avoid catching the Exception base class, or other exception superclass. If a try block might encounter more than one type of exception, write a separate catch block for each specific exception, not one catch block for an exception superclass.
• Some classes may require you to write more than one derived exception class. Write as many exception classes as needed, to distinguish between different types of situations. For example, "invalid argument value" is different from, "you don't have a needed privilege." Create different exceptions to identify different cases.
• Don't put important diagnostic information only in the text of the exception method. Create methods and members in derived exception classes as needed, to provide information to the catch block. Create an exception constructor method that takes appropriate arguments, and populate the members of the class with those arguments.
• Don't silently suppress exceptions and allow execution to continue in an erroneous state. If you catch an exception, either correct the condition or throw a new exception.
• Keep implementation-specific exceptions isolated to the appropriate layer of your application. For instance, don't propagate SQLException out of the data layer code and into business layer code.
• Don't use exceptions as a mechanism of flow control, or to return valid return values from a function.
• Clean up resources such as database connections or network connections. PHP does not support a finally block as some programming languages do, so either clean up in the catch blocks, or else design flow control outside the catch block to perform cleanup, and let execution continue after the catch.
• Use documentation from other languages for other best practices regarding using exceptions. Many of the principles are applicable, regardless of the language.

Using these standard we can write good code in PHP and PHP is now more power full language Now days lot of CMS support also comes like Drupal,joomla , Wordpress etc..

The use Keyword

classes using the full namespace still results in lengthy calls and if you are using lots of classes from the MyCompany::Blog namespace, you might not want to retype the whole path to the class every time. This is where the use keyword comes in handy. Your application will most likely use a number of different classes at any given time. Say, for example, the user creates a new post:
use MyCompany::Blog;
$user = new Blog::User();
$post = new Blog::Post();
$post->setUser( $user );
$post->setTitle( $title );
$post->setBody( $body );
$post->save();

The use keyword is not restricted to defining namespaces in which to work. You can also use it to import single classes to your file, like so:

use MyCompany::Blog::User;
$user = new User();