====== PHP example codes ====== ===== Basic Examples for reading and understanding code(testing) ===== ==== func_get_args ==== output: Hello, World! ==== reference variable ==== * Simple reference: output: 11 * Custom reference 10 && $one < 20) return $one; if($two > 10 && $two < 20) return $two; if($three > 10 && $three < 20) return $three; } $one = 2; $two = 20; $three = 15; $var = &find_variable($one, $two, $three); $var++; print "1: $one, 2: $two, 3: $three"; ?> output: 1: 2, 2: 20, 3: 16 ==== compare 2 values in the same type and value ==== ==== global variable ==== output: 120 ==== Class and Object ==== a = $b; } } class ClassTwo extends ClassOne { protected $b = 10; public function changeValue($b) { $this->b = 10; parent::changeValue($this->a + $this->b); } public function displayValues() { print "a: {$this->a}, b: {$this->b}\n"; } } $obj = new ClassTwo(); $obj->changeValue(20); $obj->changeValue(10); $obj->displayValues(); ?> output: a: 30, b: 10 ===== Send mail with PHP ===== ==== php code ==== ==== understand php email ==== === email log: /var/log/maillog === [/var/log/maillog] May 23 09:03:59 mail postfix/pickup[3000]: 12F4E40A83: uid=48 from= May 23 09:03:59 mail postfix/cleanup[3188]: 12F4E40A83: message-id=<20140523020359.12F4E40A83@mail.babies.vn> May 23 09:03:59 mail postfix/qmgr[1174]: 12F4E40A83: from=, size=569, nrcpt=1 (queue active) May 23 09:03:59 mail postfix/smtp[3190]: connect to gmail-smtp-in.l.google.com[2607:f8b0:400e:c03::1a]:25: Network is unreachable May 23 09:04:01 mail postfix/smtp[3190]: 12F4E40A83: to=, relay=gmail-smtp-in.l.google.com[74.125.25.26]:25, delay=2.2, delays=0.07/0/0.67/1.5, dsn=2.0.0, status=sent (250 2.0.0 OK 1400810645 xp1si1843220pab.44 - gsmtp) May 23 09:04:01 mail postfix/qmgr[1174]: 12F4E40A83: removed => webserver apache use local account apache to send email May 23 09:03:59 mail postfix/qmgr[1174]: 12F4E40A83: from=, size=569, nrcpt=1 (queue active) === Solve problem: error log in sendmail === Sometime occur error below: [Fri May 23 09:44:40 2014] [error] [client 115.73.56.43] PHP Warning: mail(): Could not execute mail delivery program '/usr/sbin/sendmail -t -i' in testemail.php on line 17 => Not enough memory for sending email ===== connect database with php_mysql ===== * Simple Connect * Connect and query drupal database 0) { mysql_data_seek($result,0); } while($row = mysql_fetch_array($result)) { var_dump($row); } mysql_close($link); ?> ===== check connect database with pdo_mysql ===== getMessage(); } ?> ===== Create seo link with php ===== 'a', '/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ|È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ|ë/' => 'e', '/ì|í|ị|ỉ|ĩ|Ì|Í|Ị|Ỉ|Ĩ|î/' => 'i', '/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ|Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ|ø/' => 'o', '/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ|Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ|ů|û/' => 'u', '/ỳ|ý|ỵ|ỷ|ỹ|Ỳ|Ý|Ỵ|Ỷ|Ỹ/' => 'y', '/đ|Đ/' => 'd', '/ç/' => 'c', '/ñ/' => 'n', '/ä|æ/' => 'ae', '/ö/' => 'oe', '/ü/' => 'ue', '/Ä/' => 'Ae', '/Ü/' => 'Ue', '/Ö/' => 'Oe', '/ß/' => 'ss', '/[^\s\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ', '/\\s+/' => $replacement, sprintf( '/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement ) => '' ); //Some URL was encode, decode first $title = urldecode( $title ); $map = array_merge( $map, $default ); return strtolower( preg_replace( array_keys( $map ), array_values( $map ), $title ) ); } $str = 'Rượu bàu đá'; $seolink = vietnamese_permalink($str); echo $seolink; ?> => output: ruou-bau-da ===== Read XML with PHP ===== refer: http://php.net/manual/en/book.xml.php ==== SimpleXMLElement ==== refer: * http://php.net/manual/en/class.simplexmlelement.php * http://sg2.php.net/manual/en/simplexml.examples-basic.php === Read xml file to object SimpleXMLElement === refer: http://php.net/manual/en/function.simplexml-load-file.php === Read xml string to SimpleXMLElement === SimpleXMLElement is JSON Object * First We create **example.php** file with the xml content: PHP: Behind the Parser Ms. Coder Onlivia Actora Mr. Coder El ActÓr So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 5 XML; ?> * Next we convert the xml content to SimpleXMLElement and access content of it: movie[0]->plot; ?> === Modify SimpleXMLElement Object and save xml content to file === movie[0]->plot; $movies->asXML('test.xml'); ?> ==== DOM ==== ===== Create Google Analytics Code for website ===== refer: https://support.google.com/analytics/?hl=en&rd=1#topic=1726911 ==== Add an account to your Analytics Account ==== You need to have edit permission to add accounts to an existing Google Analytics Account. To create a new account level in an existing Google Analytics Account: - Sign in to [[https://www.google.com/analytics/web/|your Google Analytics Account]]. - Click **Admin** in the menu bar at the top of any page. - From the dropdown in Account column, click **Create new account**. ==== Add a property ==== Properties are where you send data and set up reporting views Only users with edit permission can add a property to a Google Analytics account. To set up a property: - Sign in to [[https://www.google.com/analytics/web/|your Google Analytics Account]]. - Click **Admin** in the menu bar at the top of any page. - In the Account column, select the account from the dropdown that you want to add the property to.//If you have a lot of accounts, use the search box to help you find the right one//. - In the dropdown in the Property column, click **Create new property**. ===== Facebook Integration ===== ==== Basic Steps To Integrate Facebook ==== refer: * Docs: https://developers.facebook.com/docs/plugins?locale=vi_VN * Facebook comments: https://developers.facebook.com/tools/comments Steps to integrate with facebook: - Step1: Before Integrate with facebook, we need to signin facebook, then go to link https://developers.facebook.com/ and create the Your Application. Facebook will create **AppID and AppSecret** to identify the your application. We will use these informations to integrate login, like, share, comments...and manage theme - Step2: Go to link https://developers.facebook.com/docs/plugins?locale=vi_VN to get code for each features integrated. Below are basic template code for integrate like, share and comment: * Load Facebook javascript base on AppID * Update template to display button like
* update template to display button share simple:
* update template to display comment box:
- Step3: Update layout display of buttons follow link https://developers.facebook.com/docs/plugins/like-button?locale=vi_VN#faqlayout ==== Facebook Fanpage Box ==== Go to link https://developers.facebook.com/docs/plugins/page-plugin to config and create HTML code: => In case you have created other facebook integration, you **don't need to add javascript code** which generated by generate tool above ==== Facebook share Custom ==== refer: https://developers.facebook.com/docs/sharing/web - Step1: Create [[https://developers.facebook.com/docs/sharing/webmasters#markup|Open Graph Tag]] => **Facebook Crawler** will use internal heuristics to make a **best guess about the title, description, and preview image** for your content. So we need to check the **share on real website** for facebook crawl the data exactly - Step2: Go to link https://developers.facebook.com/tools/debug/ and enter the link of your website to check Open Graph Tag -> we need fix all errors when debug with this tool. With old pages,facebook has cached informations on this page, so the facebook will not recognize the Open Graph Tag update with new image. Therefore, we must use debug tools to refresh these caches -> Fix error can't comment and share missing the image(Click **Fetch New Scrape Information** 3 times to get new information). Or wait patiently for **every 30 days to ensure the properties are up to date** as it says in documentation https://developers.facebook.com/docs/plugins/like-button ==== Manage Facebook Comments(moderation tool) ==== refer: * new API v3.1: https://developers.facebook.com/docs/graph-api/reference/v3.1/comment * Docs: https://developers.facebook.com/docs/plugins/comments#moderation-setup * Moderation tool:https://developers.facebook.com/tools/comments