Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Tuesday, September 14, 2010

PHP Customized Tweeting for Twitter.com

Here is a php script which will let tweet on twitter.com providing your username, password and the message you want to tweet! Since this is a function, you can customize the code anyway you want to.


<?php

function tweet($message, $username, $password)
{
    $context = stream_context_create(array(
        'http' => array(
        'method'  => 'POST',
        'header'  => sprintf("Authorization: Basic %s\r\n", base64_encode($username.':'.$password)).
                   "Content-type: application/x-www-form-urlencoded\r\n",
        'content' => http_build_query(array('status' => $message)),
        'timeout' => 5,
    ),
    ));
    $ret = file_get_contents('http://twitter.com/statuses/update.xml', false, $context);
    return false !== $ret;
}

?>


Here is a simple code on using the tweeting function.


<?php

tweet('Php code tweeting!', 'username', 'password');

?>

Tuesday, July 27, 2010

Force Download File Using PHP Header Function

Do you want to have a web page that has a script that can make a force download.


I have an example here using PHP to push the file to the browser. To trigger the download of the file, we will use HTML header statements. In PHP, this is possible using header function in passing raw HTML headers to the browser. In this example we will pass the filename in the url for instance - www.domainname.com/download.php?file=filename.zip. Of course, you can customize the codes according to the functionality of your website.


<?php

//path directory of the file
$dir="downloads/";
//cehck if there is a file passed
if (isset($_REQUEST["file"])) {
    //concatinate the directory and the filename
    $file=$dir.$_REQUEST["file"];
    //set HTML headers that must be sent to the browser.
    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();
    //read the file
    readfile($file);
    exit;
}
else {
    //prompt if no file selected
    echo "No file selected";
}

?>


Click here for the php header manual