Category

PHP Recipes

28 OCT

Backup of MySQL database with a PHP script

Sometimes you need to make a mysqldump backup without having SSH access or PHPMyAdmin. It can be done with PHP by means of SHOW TABLES/SHOW CREATE TABLE mysql queries. Below is an improved solution proposed here.

Improvements:

  • Got rid of memory_limit – writing to file on every iteration
  • gzip support – you don’t have to download huge uncompressed .sql
  • PHP notice fixed
  • added IF EXISTS to DROP TABLE
  • ereg_replase -> str_replace
  • added set_time_limit(0) to work with large DB’s

In order to make a dump:

Read more


7 MAR

USPS Web Tools guide

Besides its primary role – delivering mail, USPS allows several other functions by means of http://www.usps.com/webtools/. In order to use them you’ll need to make xml in conformance with USPS Web Tools documentation and send it as for example cURL request. Below given is a class that will handle all these cURL requests. It’s the class serving all the requests in this article. We’ll need to pass it only URL, API method name and the XML.

//connect to USPS
class USPS {
	public function connectToUSPS($url, $api, $xml) {
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $api . $xml);
		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

		$result = curl_exec($ch);
		$error = curl_error($ch);

		if (empty($error)) {
			return $result;
		} else {
			return false;
		}
	}
}

Read more