howtos » 2007 » April
mysqldump is a useful utility for dumping the contents of an entire database to an SQL file.
-
mysqldump --user=dbusername --password=dbpassword db_name_here> db_name_here.sql
By default the --opt option is enabled, which is a shorthand option for specifying the below options. According to the documentation, using this option "... should give you a fast dump operation and produce a dump file that can be reloaded into a MySQL server quickly".
-
--add-drop-table
-
--add-locks
-
--create-options
-
--disable-keys
-
--extended-insert
-
--lock-tables
-
--quick
-
--set-charset
cURL is used to interact with remote URLs without needing a user to initiate the process (e.g. by clicking on a form submit button). cURL is useful for submitting HTTP POST/PUT/DELETE requests when dealing with web services. PHP has inbuilt cURL support since PHP 4.0.2 using cURL's libcurl library. Find out more about PHP/CURL -- using libcurl with PHP here.
A fullset of cURL options and what they mean can be found in
PHP's manual entry for curl_setopt().
-
//Contains encoded string to pass along for basic authentication purposes
-
-
//Target URL - the URL you want to submit a form to
-
$target_url = 'http://www.remotesite.com/post_target.php';
-
-
//Create a new cURL handle
-
//Passing the target URL to curl_init allows you to bypass the call curl_setopt($ch, CURLOPT_URL, $target_url);
-
$ch = curl_init($target_url);
-
-
//Tell the handler that the info is to be sent using an HTTP POST request
-
curl_setopt($ch, CURLOPT_POST, true);
-
-
//Set other relevant headers. Place each header as an array element
-
//An alternative to building the Authorization header is to use curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
-
'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3');
-
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-
-
//Pass the POST fields - be sure to urlencode your value strings (hint: http_build_query() will do this for you; PHP5)
-
//Below we assume values have already been posted to this script and kept in $_POST. We have validated the submission and
-
// are now posting the same values to a remote URL
-
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
-
-
//When we execute the handle, we want curl_exec() to return to a string rather than directly outputting it
-
curl_setopt($ch, CURLOPT_RETURNTRANSER, true);
-
-
//Don't use a cached connection - explicitly create a new one
-
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
-
-
//Fail if cannot connect to the target server within 5 seconds
-
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
-
-
//If the target server returns a redirect request using the "Location:" header directive, then follow it.
-
//To prevent recursive redirects, only do a max of 5 follows
-
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
-
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
-
-
//Let's now execute the handler
-
//Because CURLOPT_RETURNTRANSFER is true, we need to capture the return value of curl_exec()
-
$response_data = curl_exec($ch);
-
-
//Was there an error?
-
//curl_errno() returns the error code
-
//curl_error() returns a clear text message for the last cURL operation
-
if (curl_errno($ch)> 0){
-
} else {
-
//Close the handler and release resources
-
curl_close($ch);
-
}
-
-
-
//Now do something with your data
-
return myProcessingFunction($data);
-
#Activate the rewrite engine if it isn't so already
-
RewriteEngine on
-
#intercept all HTTP requests to the site that do not have a 'www.' at the beginning of the Domain name
-
#and then force a redirect to the same page address only this time, including the 'www.'
-
RewriteCond %{HTTP_HOST} !^www\..* [NC]
-
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]
-
#The inverse of the above rule - intercept HTTP requests with a 'www.' at the start and redirect to the non-www version
-
# Ensure that your serverName directive is set up to the non-www name of your domain
-
RewriteCond %{HTTP_HOST} ^www\..* [NC]
-
RewriteRule ^(.*) http://%{SERVER_NAME}/$1 [R=301]
Sending a plain-text e-mail through PHP is a simple process
The $to parameter can look like:
example@mysite.com example@mysite.com, user@mysite.com Example Sr <example@mysite.com>, Example Jr <user@mysite.com>
Common headers to use in your messages:
-
$headers = "From: yourname@yoursite.com\r\n
-
Reply-To: replyhere@yoursite.com\r\n
-
Cc: watcher@yoursite.com\r\n
-
Bcc: spy@yoursite.com\r\n
-
X-Mailer: YourApplicationNameHere\r\n"
htpasswd is a command line tool for creating and updating user authentication files. These files are primarily used by apache to authenticate HTTP users.
Creating a password file
-
htpasswd -c /path/to/.htpasswd username
Adding a user & password
-
htpasswd -b /path/to/.htpasswd username password
PHP 5's DOM Functions (or extension) allows you to create and manipulate XML documents. In fact, it can also be used to create and manipulate any documents adhering to the DOM3 specifications such as HTML and XHTML documents.
Creation of a DOM document is a fairly simple affair
- Instantiate DOMDocument - also specify which version of XML the document is in and how it is encoded
- Create elements through the DOMDocument object
- Set your elements' properties (e.g. values, attributes, child nodes)
- Append the elements to your parent DOMDocument
- Output as fully-formed XML using
DOMDocument::saveXML()
The following example shows you how to create an XML document (using UTF-8 encoding) using PHP 5's DOM functions.
-
//First create an XML document
-
//The following statement sets XML version 1.0, encoding utf-8
-
$dom = new DOMDocument('1.0', 'utf-8');
-
-
//Step 1: create the root node
-
//Now create the root node and declare the XML namespace
-
$entry_node = $dom->createElementNS('http://www.w3.org/2005/Atom', 'entry');
-
//If you need to add additional namespace declarations, do it now
-
$entry_node->setAttribute('xmlns:gd','http://schemas.google.com/g/2005');
-
-
//Step 2: create all child nodes
-
$category_node = $dom->createElement('category');
-
$category_node->setAttribute('scheme', 'http://schemas.google.com/g/2005#kind');
-
$category_node->setAttribute('term', 'http://schemas.google.com/g/2005#event');
-
-
$title_node = $dom->createElement('title', $title);
-
$title_node->setAttribute('type', 'text');
-
-
$content_node = $dom->createElement('content', $content);
-
$content_node->setAttribute('type', 'text');
-
-
//In some cases the child node may itself be a parent with its own child nodes
-
//Create the parent node
-
$author_node = $dom->createElement('author');
-
-
//Create the child nodes
-
$author_name_node = $dom->createElement('name', $author_name);
-
$author_email_node = $dom->createElement('email', $author_email);
-
-
//Add the child nodes to the parent
-
$author_node->appendChild($author_name_node);
-
$author_node->appendChild($author_email_node);
-
-
$transparency_node = $dom->createElement('gd:transparency');
-
$transparency_node->setAttribute('value', 'http://schemas.google.com/g/2005#event.opaque');
-
-
$eventstatus_node = $dom->createElement('gd:eventStatus');
-
$eventstatus_node->setAttribute('value', 'http://schemas.google.com/g/2005#event.confirmed');
-
-
$where_node = $dom->createElement('gd:where');
-
$where_node->setAttribute('valueString', $where);
-
-
$when_node = $dom->createElement('gd:when');
-
$when_node->setAttribute('startTime', $startTime . '+10:00');
-
$when_node->setAttribute('endTime', $endTime . '+10:00');
-
-
//Another child node which is also a parent
-
$reminder_node = $dom->createElement('gd:reminder');
-
$reminder_node->setAttribute('minutes', $minutes);
-
-
$when_node->appendChild($reminder_node);
-
-
//Step 3: Add child nodes to the parent node
-
$entry_node->appendChild($category_node);
-
$entry_node->appendChild($title_node);
-
$entry_node->appendChild($content_node);
-
$entry_node->appendChild($author_node);
-
$entry_node->appendChild($transparency_node);
-
$entry_node->appendChild($eventstatus_node);
-
$entry_node->appendChild($where_node);
-
$entry_node->appendChild($when_node);
-
-
-
//Append the root node to the document
-
$dom->appendChild($entry_node);
-
-
//Return the fully-formed XML representation of the DOMDocument
-
return $dom->saveXML();
Here's how to include another template file within a template file:
If you have successfully obtained a calendar feed, you should receive some XML looking like this
-
<default:feed>
-
<default:id>http://www.google.com/calendar/feeds/vinoajtest%40gmail.com/public/basic</default:id>
-
<default:updated>2007-04-08T03:38:29.000Z</default:updated>
-
<default:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2005#event"/>
-
<default:title type="text">Vinoaj Vijeyakumaar</default:title>
-
<default:subtitle type="text">Vinoaj Vijeyakumaar</default:subtitle>
-
<default:link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://www.google.com/calendar/feeds/vinoajtest%40gmail.com/public/basic"/>
-
<default:link rel="self" type="application/atom+xml" href="http://www.google.com/calendar/feeds/vinoajtest%40gmail.com/public/basic?max-results=25"/>
-
<default:author>
-
<default:name>Vinoaj Vijeyakumaar</default:name>
-
<default:email>vinoajtest@gmail.com</default:email>
-
</default:author>
-
<default:generator version="1.0" uri="http://www.google.com/calendar">Google Calendar</default:generator>
-
<openSearch:totalResults>1</openSearch:totalResults>
-
<openSearch:startIndex>1</openSearch:startIndex>
-
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
-
<gCal:timezone value="Australia/Sydney"/>
-
<default:entry>
-
<default:id>
-
http://www.google.com/calendar/feeds/vinoajtest%40gmail.com/public/basic/12ps2qhasoejgtf8uo89des4f0
-
</default:id>
-
<default:published>2007-04-08T03:35:52.000Z</default:published>
-
<default:updated>2007-04-08T03:36:23.000Z</default:updated>
-
<default:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2005#event"/>
-
<default:summary type="html">When: Mon Apr 9, 2007<br></default:summary>
-
<default:link rel="alternate" type="text/html" href="http://www.google.com/calendar/event?eid=MTJwczJxaGFzb2VqZ3RmOHVvODlkZXM0ZjAgdmlub2FqdGVzdEBt" title="alternate"/>
-
<default:link rel="self" type="application/atom+xml" href="http://www.google.com/calendar/feeds/vinoajtest%40gmail.com/public/basic/12ps2qhasoejgtf8uo89des4f0"/>
-
</default:entry>
-
</default:feed>
A fully-formed private event looks something like this
-
<entry>
-
<id>http://www.google.com/calendar/feeds/vinoajtest%40gmail.com/private/full/12ps2qhasoejgtf8uo89des4f0</id>
-
<published>2007-04-08T03:35:52.000Z</published>
-
<updated>2007-04-08T03:36:23.000Z</updated>
-
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2005#event"/>
-
<title type="text">Easter Monday</title>
-
<content type="text"/>
-
<link rel="alternate" type="text/html" href="http://www.google.com/calendar/event?eid=MTJwczJxaGFzb2VqZ3RmOHVvODlkZXM0ZjAgdmlub2FqdGVzdEBt" title="alternate"/>
-
<link rel="self" type="application/atom+xml" href="http://www.google.com/calendar/feeds/vinoajtest%40gmail.com/private/full/12ps2qhasoejgtf8uo89des4f0"/>
-
<author>
-
<name>Vinoaj Vijeyakumaar</name>
-
<email>vinoajtest@gmail.com</email>
-
</author>
-
<gd:visibility value="http://schemas.google.com/g/2005#event.public"/>
-
<gCal:sendEventNotifications value="false"/>
-
<gd:transparency value="http://schemas.google.com/g/2005#event.transparent"/>
-
<gd:comments>
-
<gd:feedLink href="http://www.google.com/calendar/feeds/vinoajtest%40gmail.com/private/full/12ps2qhasoejgtf8uo89des4f0/comments"/>
-
</gd:comments>
-
<gd:eventStatus value="http://schemas.google.com/g/2005#event.confirmed"/>
-
<gd:where/>
-
<gd:when startTime="2007-04-09" endTime="2007-04-10">
-
<gd:reminder minutes="10"/>
-
</gd:when>
-
</entry>
The following steps show you how to grab a feed using Zend Framework's GData package and then grab the appropriate values.
-
//Initiate an authorised http client
-
//See previous post on how to get an auth token and convert it to a session token
-
$client = Zend_Gdata_AuthSub::getHttpClient($authToken);
-
-
//Instantiate a new GData Calendar instance + set the identifiers and parameters
-
$gdata_cal = new Zend_Gdata_Calendar($client);
-
$gdata_cal->setUser('vinoajtest@gmail.com');
-
$gdata_cal->setVisibility('private');
-
$gdata_cal->setProjection('full');
-
-
//Returns an atom feed object represented by Zend_Feed_Atom
-
//The Zend_Feed_Atom object is an extension of DOMDocument
-
//Call Zend_Feed_Atom::saveXML() to acquire a fully-formed DOMDocument at any time
-
$gdata_cal_feed = $gdata_cal->getCalendarFeed();
-
-
//Let's spit out some basic FEED information
-
$n_entries = $gdata_cal_feed->count(); //int
-
$feed_title = $gdata_cal_feed->title(); //str
-
$feed_id = $gdata_cal_feed->id(); //str url
-
-
$feed_links = $gdata_cal_feed->link(); //array links
-
foreach ($feed_links as $ix => $link){
-
//Each item is of type DOMElement
-
$link_rel = $link->getAttribute('rel');
-
$link_type = $link->getAttribute('type');
-
$link_href = $link->getAttribute('href');
-
}
-
-
$feed_subtitle = $gdata_cal_feed->subtitle(); //Equivalent to a RSS' channel description
-
$feed_author_name = $gdata_cal_feed->author->name(); //str
-
$feed_author_email = $gdata_cal_feed->author->email(); //str email
-
-
//Now lets's spit out ENTRIES and their information
-
foreach ($gdata_cal_feed as $ix => $cal_entry){
-
$cal_entry_id = $cal_entry->id(); //str url
-
$cal_entry_title = $cal_entry->title(); //str
-
$cal_entry_author = $cal_entry->author->name(); //str
-
$cal_entry_content = $cal_entry->content(); //str
-
$cal_entry_email = $cal_entry->author->email(); //str email
-
$cal_entry_published = $cal_entry->published(); //str date
-
$cal_entry_updated = $cal_entry->updated(); //str date
-
$cal_entry_summary = $cal_entry->summary(); //str
-
$cal_entry_visibility = $cal_entry->{'gd:visibility'}['value']; //str bool
-
$cal_entry_sendeventnotify = $cal_entry->{'gCal:sendEventNotifications'}['value'];
-
$cal_entry_transparency = $cal_entry->{'gd:transparency'}['value']; //str
-
$cal_entry_event_status = $cal_entry->{'gd:eventStatus'}['value']; //str
-
$cal_entry_where = $cal_entry->{'gd:where'}['valueString']; //str
-
$cal_entry_starttime = $cal_entry->{'gd:when'}['startTime']; //str date
-
$cal_entry_endtime = $cal_entry->{'gd:when'}['endTime']; //str date
-
$cal_entry_reminder_mins = $cal_entry->{'gd:when'}->{'gd:reminder'}['minutes']; //str mins
-
$cal_entry_commentsfeedlink = $cal_entry->{'gd:comments'}->{'gd:feedLink'}['href']; //str url
-
-
$cal_entry_links = $cal_entry->link();
-
foreach ($cal_entry_links as $ix => $entry_link){
-
$entry_link_rel = $entry_link->getAttribute('rel'); //str
-
$entry_link_type = $entry_link->getAttribute('type'); //str
-
$entry_link_href = $entry_link->getAttribute('href'); //str url
-
$entry_link_title = $entry_link->getAttribute('title'); //str
-
}
-
-
//category
-
$cal_entry_category = $cal_entry->{'category'}; //Type Zend_Feed_Entry
-
$cal_entry_category_dom = $cal_entry_category->getDOM();