Archive for the ‘PHP’ Category

20
Apr

Disk access is slow. Network access is slow. Databases typically use both.

Memory is fast. Using a local cache avoids the overhead of network and disk access. Combine these truths and you get memcached, a “distributed memory object caching system” originally developed for the Perl-based blogging platform LiveJournal.

If your application isn’t distributed across multiple servers, you probably don’t need memcached. Simpler caching approaches — serializing data and storing it in a temporary file, for example — can eliminate a lot of redundant work on each request. In fact, this is the sort of low-hanging fruit we consider when helping our clients tune their apps.

One of the easiest and most universal ways to cache data in memory is to use the shared memory helpers in APC, a caching system originally developed by our colleague George Schlossnagle. Consider the following example:

<?php
$feed = apc_fetch('news');

if ($feed === FALSE) {

$feed = file_get_contents('http://example.org/news.xml');

// Store this data in shared memory for five minutes.

apc_store('news', $feed, 300);

}

// Do something with $feed.

?>

With this type of caching, you don’t have to wait on a remote server to send the feed data for every request. Some latency is incurred — up to five minutes in this example — but this can be adjusted to as close to real time as your app requires.

,

30
Mar

We can randomly collect one or more element from an array by using array_rand() function in PHP. We can specify the number of random elements required by specifying an optional parameter in side the function.

$value= array("Rabin","Reid","Cris","KVJ","John");

$rand_keys=array_rand($value,2);
echo "First random element = ".$value[$rand_keys[0]];
echo "<br>Second random element = ".$value[$rand_keys[1]];

30
Mar
<?php

$to = "recipient@example.com";

$subject = "Hi!";

$body = "Hi,\n\nHow are you?";

if (mail($to, $subject, $body)) {

echo("<p>Message successfully sent!</p>");

} else {

echo("<p>Message delivery failed...</p>");

}

?>

30
Mar
<?php
header("Content-type: image/png");

$text = imagecreatefromgif("site.gif");
$lightbulb = imagecreatefrompng("lightbulb.png");
list($width, $height) = getimagesize("site.gif");

imagecopymerge($lightbulb, $text, 0,10, 0,0, $width, $height, 70);

imagepng($lightbulb);
imagedestroy($lightbulb);

/>

31
Jan
if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod')) {
    header('Location: http://yoursite.com/iphone');
    exit();
}

24
Jan

The following code snippet will upload your image to facebook album:

$file = "my_image.png";

$args = array(
'access_token' => 'your facebook access token',
'message' =>  'Your message here',
'picture' => '@' . realpath($file),
'image' => '@' . realpath($file)
);

$target_url = "https://graph.facebook.com/<album_id>/photos";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
curl_close($ch);

Next is to decode the $result, then you will get an Id in return.

$result_obj = json_decode($result);

echo $result_ob->id;

22
Dec

Try this code only if you have facebook access token:

$access_token = "Paste facebook access token";
//give path like following if your image file on the server is in the same directory where this script file is stored.
$file = "./cover.jpg";
$args = array(
'access_token' => $access_token,
'name' => 'Event Title',
'description' => 'Event Description',
'location' => "location details"',
'start_time' => '2020-01-01T12:00:00+0000',
'end_time' => '2020-01-02T12:00:00+0000',
'picture' => '@' . realpath($file),
'image' => '@' . realpath($file)
);

$target_url = "https://graph.facebook.com/me/events";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$result = curl_exec($ch);
curl_close($ch);

echo "Event ID= " . $result;

Facebook will return an event id, which can be used to see the event.  The url to your event will be like following:

http://www.facebook.com/events/<event id>

That’s it.

16
Nov

Not sure why posting a check-in on Facebook is such a rare topic on Google – I would expect it to be one of the most written about and searched about topics.
Anyway after hours of Google-ing, trials using graph api and legacy REST api, this is the simplest code (my opinion) to post or do or make a check-in on Facebook.

Assuming that you have the latest Facebook PHP SDK (https://github.com/facebook/php-sdk/downloads) copy this code:
checkin.php

require("src/facebook.php");

// construct the object with your facebook app data
$facebook = new Facebook(array(
'appId'  => '[YOUR APP ID]',
'secret' => '[YOUR APP SECRET ID]',
'cookie' => true
));

try {
	// to get the id of the currently logged in user
	// if, you want you can manually set a user id here like this:
	//$uid = '[FB USER ID]';
	$uid = $facebook->getUser();

	// if you know know the access token before hand then you can set it here
	// or you can leave this line commented
	//$facebook->setAccessToken([ACCESS TOKEN FOR THIS USER - APP]);

	$api_call = array(
		'method' => 'users.hasAppPermission',
		'uid' => $uid,
		'ext_perm' => 'publish_checkins'
	);
	$can_post = $facebook->api($api_call);
	if ($can_post) {
		$facebook->api('/'.$uid.'/checkins', 'POST', array(
		'access_token' => $facebook->getAccessToken(),
		'place' => '[LOCATION ID]',
		'message' => 'I am place to check in',
		'picture' => 'http://test.com/someplace.png',
		'coordinates' => json_encode(array(
		   'latitude'  => '[LATITUDE]',
		   'longitude' => '[LONGITUDE]',
		   'tags' => '[A LIST OF TAGS TO USE FOR THIS CHECKIN]'))
		));
		echo 'You are checked in';
	} else {
		die('Permissions required!');
	}
} catch (Exception $e){
	// No user found - ask the person to login
	$login_url = $facebook->getLoginUrl();
	header("Location: ".$login_url);
}

Some points to note:

  • If, you are setting an access token manually, then ensure that the user has allowed publish_checkins permission.
  • You must use a valid Location ID that has a page on Facebook
  • The URL to this script must be in the app site Url. For example, if your Facebook app’s site URL is http://myworld.com/ then this script should be somewhere inside http:/myworld.com

, , , , , , , , , , , , , , , , , , ,

31
Oct
<?php
function byVal($arg) {
    echo 'As passed     : ', var_export(func_get_arg(0)), PHP_EOL;
    $arg = 'baz';
    echo 'After change  : ', var_export(func_get_arg(0)), PHP_EOL;
}

function byRef(&$arg) {
    echo 'As passed     : ', var_export(func_get_arg(0)), PHP_EOL;
    $arg = 'baz';
    echo 'After change  : ', var_export(func_get_arg(0)), PHP_EOL;
}

$arg = 'bar';
byVal($arg);
byRef($arg);
?>

, , , ,

31
Oct
<?php

$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

, , , , , , , , , , , , , , ,