Sunday, 30 November 2014

PHP file_get_contents Function - Reads entire file into a string

string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )
Reads entire file into a string
This function is similar to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to maxlen bytes. On failure, file_get_contents() will return FALSE.

<?php
$homepage 
file_get_contents('http://www.example.com/');
echo 
$homepage;?>

5.1.0 Added the offset and maxlen parameters. 5.0.0 Added context support.

PHP mail attachment - sending an attachment with PHP

PHP utf8_encode Function - Encodes an ISO-8859-1 string to UTF-8

string utf8_encode ( string $data )
Encodes an ISO-8859-1 string to UTF-8
This function encodes the string data to UTF-8, and returns the encoded version. UTF-8 is a standard mechanism used by Unicode for encoding wide character values into a byte stream. UTF-8 is transparent to plain ASCII characters, is self-synchronized (mea

Walk through nested arrays/objects and utf8 encode all strings.



<?php// Usageclass Foo {

    public
$somevar = 'whoop whoop';

}
$structure = array(
    
'object' => (object) array(
        
'entry' => 'hello wörld',
        
'another_array' => array(
            
'string',
            
1234,
            
'another string'
        
)
    ),
    
'string' => 'foo',
    
'foo_object' => new Foo);
utf8_encode_deep($structure);
// $structure is now utf8 encodedprint_r($structure);
// The functionfunction utf8_encode_deep(&$input) {
    if (
is_string($input)) {
        
$input = utf8_encode($input);
    } else if (
is_array($input)) {
        foreach (
$input as &$value) {
            
utf8_encode_deep($value);
        }
        unset(
$value);
    } else if (
is_object($input)) {
        
$vars = array_keys(get_object_vars($input));
        foreach (
$vars as $var) {
           
utf8_encode_deep($input->$var);
        }
    }
}
?>

1 7 0bbbbbbb 2 11 110bbbbb 10bbbbbb 3 16 1110bbbb 10bbbbbb 10bbbbbb 4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb

Corrigindo erros de acentuação no PHP

Saturday, 29 November 2014

com_create_guid Generate a globally unique identifier

string com_create_guid ( void )
Generate a globally unique identifier (GUID)
Generates a Globally Unique Identifier (GUID).

The phunction PHP framework (http://sourceforge.net/projects/phunction/) uses the following function to generate valid version 4 UUIDs:



<?phpfunction GUID()
{
    if (
function_exists('com_create_guid') === true)
    {
        return
trim(com_create_guid(), '{}');
    }
    return
sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));

}
?>

The output generated by the sprintf() and mt_rand() calls is identical to com_create_guid() results.


Friday, 28 November 2014

spl_autoload What is spl_autoload function in php

spl_autoload What is spl_autoload function in php

void spl_autoload ( string $class_name [, string $file_extensions = spl_autoload_extensions() ] )
Default implementation for __autoload()
This function is intended to be used as a default implementation for __autoload(). If nothing else is specified and spl_autoload_register() is called without any parameters then this function will be used for any later call to __autoload().
spl_autoload_register() allows you to register multiple functions (or static methods from your own Autoload class) that PHP will put into a stack/queue and call sequentially when a "new Class" is declared.

Following is the example
 
<?php
spl_autoload_register('myAutoloader');
function loadAutoload($className){
    $path = '/path/to/class';
    include $path.$className.'.php';
}
$obj = new Zend_Log();
?>

Thursday, 27 November 2014

parse_ini_string - Parse a configuration string

array parse_ini_string ( string $ini [, bool $process_sections = false [, int $scanner_mode = INI_SCANNER_NORMAL ]] )
Parse a configuration string
parse_ini_string returns the settings in string ini in an associative array.

Function parse_ini_file doesn't parse a remote ini file if allow_url_include is off. But if allow_url_fopen is on, you can use parse_ini_string to parse a remote ini file after read its contents.
/**
* Assume that; allow_url_include=0 and allow_url_fopen=1
* (default values in php.ini)
*/

$iniUrl = 'http://website.com/remote/config.ini';
/**
* Warning: parse_ini_file(): http:// wrapper is disabled in the server configuration by allow_url_include=0
*/
$config = parse_ini_file($iniUrl);

/**
* works fine
*/
$config = parse_ini_string(file_get_contents($iniUrl));

Wednesday, 26 November 2014

stream_socket_client - Open Internet or Unix domain socket connection

resource stream_socket_client ( string $remote_socket [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") [, int $flags = STREAM_CLIENT_CONNECT [, resource $context ]]]]] )
Open Internet or Unix domain socket connection
Initiates a stream or datagram connection to the destination specified by remote_socket. The type of socket created is determined by the transport specified using standard URL formatting: transport://target. For Internet Domain sockets (AF_INET) such as T

<?php
$fp 
stream_socket_client("tcp://www.example.com:80"$errno$errstr30);
if (!
$fp) {
    echo 
"$errstr ($errno)<br />\n";
} else {
    
fwrite($fp"GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
    while (!
feof($fp)) {
        echo 
fgets($fp1024);
    }
    
fclose($fp);
}
?>


Tuesday, 25 November 2014

imagecopyresampled-Copy and resize part of an image with resampling

bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
Copy and resize part of an image with resampling
imagecopyresampled() copies a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.

<?php// The file

$filename 'test.jpg';
$percent 0.5;// Content type
header('Content-Type: image/jpeg');// Get new dimensions
list($width$height) = getimagesize($filename);$new_width $width $percent;$new_height $height $percent;// Resample
$image_p imagecreatetruecolor($new_width$new_height);
$image imagecreatefromjpeg($filename);
imagecopyresampled($image_p$image0000$new_width$new_height$width$height);// Output
imagejpeg($image_pnull100);?>


Monday, 24 November 2014

Mysql Privileges - Types of privileages in MySql - How do I grant privileges in MySQL

Mysql Privileges - Types of privileages in MySql - How do I grant privileges in MySQL

There are three types of privileges in MySql

  1. Administrative privilege(s) enable users to manage operation of MySQL server. These are global because they are not specific to a particular mysql database. 
  2. Database privileges apply to a database and to all objects within it, means apply to specific database. 
  3. Privileges for database object(s) such as table(s), indexe(s), stored routine(s), and view(s). These privileges apply table, index, Stored Procedure, views etc. of a single database.  It can be granted for specific objects within a database OR for all objects of a given type within a database.


Following are  permissible Privileges for GRANT and REVOKE
PrivilegeColumnContext
CREATECreate_privdatabases, tables, or indexes
DROPDrop_privdatabases, tables, or views
GRANT OPTIONGrant_privdatabases, tables, or stored routines
LOCK TABLESLock_tables_privdatabases
REFERENCESReferences_privdatabases or tables
EVENTEvent_privdatabases
ALTERAlter_privtables
DELETEDelete_privtables
INDEXIndex_privtables
INSERTInsert_privtables or columns
SELECTSelect_privtables or columns
UPDATEUpdate_privtables or columns
CREATE TEMPORARY TABLESCreate_tmp_table_privtables
TRIGGERTrigger_privtables
CREATE VIEWCreate_view_privviews
SHOW VIEWShow_view_privviews
ALTER ROUTINEAlter_routine_privstored routines
CREATE ROUTINECreate_routine_privstored routines
EXECUTEExecute_privstored routines
FILEFile_privfile access on server host
CREATE USERCreate_user_privserver administration
PROCESSProcess_privserver administration
RELOADReload_privserver administration
REPLICATION CLIENTRepl_client_privserver administration
REPLICATION SLAVERepl_slave_privserver administration
SHOW DATABASESShow_db_privserver administration
SHUTDOWNShutdown_privserver administration
SUPERSuper_privserver administration






Following are mysql grant permission example:

GRANT SELECT ON databasename.* TO user@'localhost';
/* Give the select permission to user i.e user for database databasename*/

GRANT SELECT ON databasename.* TO user@'localhost' IDENTIFIED BY 'password';
/* Give the select permission to user i.e user for database databasename and create the user*/

GRANT SELECT, INSERT, DELETE ON databasename TO username@'localhost' IDENTIFIED BY 'password';
/* Give the SELECT, INSERT, DELETE permission to user i.e user for database databasename*


select * from mysql.user where User='username';
/*To see a list of the privileges that have been granted to a specific user:*/


GRANT all privileges ON databasename.* TO user@'localhost';
/* Give all permissions to user i.e user for database databasename*/




How to setup your twitter App?

How to setup your twitter App?

Setup your App in Twitter.com

  1. Login to https://twitter.com (If Not registered, please register first)
  2. Add Mobile Number and verify (https://twitter.com/settings/devices)
  3. Go to Manage Apps page (i.e https://apps.twitter.com/)
  4. Create a New App
  5. Fill all required info
  6. In "What type of access does your application need" set "Read and write"
  7. Save API Key and API secret (Consumer key and Consumer Secret key)


Follow PHP Work from Below sites(I have test this, Its working)



Friday, 14 November 2014

Current time zone for a city- Free API

Current time zone for a city- Free API

Get TimeZone & Local time from Latitude/Longitude



$localtime = 0;
$latitude = '40.71417';
$longitude = '-74.00639';
try {
    $client = new Zend_Http_Client('http://www.earthtools.org/timezone-1.1/' . $latitude . '/' . $longitude);
    $response = $client->request('GET');
    $xmlData = $response->getBody();
    if (!empty($xmlData)) {
        $xmlData = simplexml_load_string($xmlData);  
        print_r($xmlData);
    }

} catch (Exception $e) {
    echo 'Error' . $e->getMessage();
}

Output:

SimpleXMLElement Object
(
    [version] => 1.0
    [location] => SimpleXMLElement Object
        (
            [latitude] => 40.71417
            [longitude] => -74.00639
        )
    [offset] => -5
    [suffix] => R
    [localtime] => 13 Nov 2014 01:42:07
    [isotime] => 2014-11-13 01:42:07 -0500
    [utctime] => 2014-11-13 06:42:07
    [dst] => Unknown
)


Get Latitude/Longitude from address


http://maps.googleapis.com/maps/api/geocode/json?address=chandigarh&sensor=false

Output:
http://maps.googleapis.com/maps/api/geocode/json?address=chandigarh&sensor=false