Archive for the ‘PHP’ Category

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;
}
?>

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

30
Oct

getMessage() 

gets the exception’s message

 
 

getCode(

returns a numerical code that represents the exception

 
 

getFile()

returns the file where the exception was thrown

 
 

getLine() 

returns the line number in the file where the exception was thrown

 
 

getTrace()

returns the an array of the backtrace() before the exception was thrown

 
 

getPrevious() 

returns the exception thrown before the current one, if any

 
 

getTraceAsString()

returns the backtrace() of an exception as a string instead of an array

 
 

__toString() 

returns the whole exception as a string. Is overrideable.

 
 

 
 

, , , , , , , , ,

19
Oct

A php custom function to check the argument is number or string:

function is_number_or_string($val){

if( ((int) $val + 0 ) > 0 ){
return "Number";
}else{
return "String";
}
}

, , , ,

04
Oct

The following function will read all files with .txt extension and return an array that contains all files.

function readAllTextFiles($directory_name){
$arr_files = array();
if ($handle = opendir('.')) {
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
if( preg_match("/.txt$/i", $file) ){
$arr_files[] = $file;
}
}
closedir($handle);
}
return $arr_files;
}

, , , ,

17
Aug

You can create tables with css without using <table>, <tr>, <td> tags:

<html><title>CSS Table</title>
<style type="text/css">
.main{
float:left;
height:540px;
width: 800px;
}
.form_style{
clear:both;
}
.cell{
height:60px;
width:60px;
color:#dddddd;
border: 1px solid #999999;
text-align:center;
float:left;
}
.cell-last{
height:60px;
width:60px;
color:#dddddd;
border: 1px solid #999999;
text-align:center;
float:left;
clear:left;
}
</style>
</script>
</head>
<body>
<div>
<?php for ($i=0; $i<8; $i++) {
for( $j=0; $j<10; $j++ ){
if( $i > 0 && $j == 0 ){
?>
<div id="cell_<?php echo $i.'_'.$j; ?>"></div>

<?php }else{ ?>

<div id="cell_<?php echo $i.'_'.$j;?>"></div>
<?php         }
}
}
?>
</div>
</body>
</html>

, , , , , , ,