Skip to content

My PHP , Wordpress and Linux Lab

Archive

December, 2006Archive for

Go Flight!

Dec 19
Posted by : Sabeen Malik in RC Airplanes

Ok so yeah , it wasnt my fault after all , we got the crystals changed and that took care of all the glitches. I heard that it was flying pretty good after that , I personally didnt dare to fly after the crash , didnt want to spoil Gohars’ fun again.

So after a couple of casual visits i decided to get back in the pilots seat . took the plane for a ride and it was pretty good. Managed to pull a couple of decent rolls and some pretty neat turns. Gohars first flight was an encouragement , he seems to do some neat tricks with the plane now. I think we both now know that this trainers days are numbered , we are hungry for some real tricky flying now , this one seems to have outlived our expectations.

These are a couple of functions i use to get the directories and files from a specific folder. This is a recursive function and brings the sub directories as well. Returns an array.

First param is the folder path to start
Second param tells the function if it needs to bring the files inside the folder
Third param tells if the function needs to bring sub directories as well.

$folders = array();
folderlist(”testserver” , true , true);
print_r($folders);

function folderlist($folder_path = “” , $getfiles = true , $tree=true){
  global $folders;
  $handle = opendir($folder_path.”/.”);
  while (false !== ($file = readdir($handle))) {
   if ($file != “.” && $file != “..”) {
    if(is_dir($folder_path.”/”.$file)){
    if($getfiles){
      $fileshere = filelist($folder_path.”/”.$file);
       $folders[] = array(”foldername” => $folder_path.”/”.$file  ,
                        “files” => $fileshere
                        );
     }else{
     $folders[] = array(”foldername” => $folder_path.”/”.$file);
     }
     if($tree) folderlist($folder_path.”/”.$file);
  }
 } } return $folders;
}

function filelist($folder_path){
  $files = array();
  $handle = opendir($folder_path.”/.”);
  while (false !== ($file = readdir($handle))) {
    if ($file != “.” && $file != “..”) {
     if(!is_dir($folder_path.”/”.$file)){
      $files[] = $folder_path.”/”.$file;
     }
    }
  }
  return $files;
}

 

How to Easily Use CURL with PHP

Dec 4
Posted by : Sabeen Malik in CURL, PHP, Web World

My Simple CURL Wrapper Class

Alot of times i have to use CURL with PHP for getting data from remote sites. Its a wonderfull library which makes alot of things easier for developers who want to interact or get data from remote sites and use them inside their own code. This is where my class comes in handy :)

For simpler uses you can get data easily simply by:
$data = implode(”" , file(”http://www.abc.com/page1.html”));

But what to do when you have to login to another site and get data from inside the secure page?

So here is what i do with this class :

include(’curl.class.php’);
$browser = new extractor(true);

$loginpost['username'] = “xyz”;
$loginpost['password'] = “xyz”;
$formaction = “http://www.abc.com/login.php”;
$somesecurepage = “http://www.abc.com/myaccount.php”;

$data = $browser->getdata($formaction , $loginpost , “” , true , false);
$mypage = $browser->getdata($somesecurepage , array() , “” , true , true);

and most of the time i end up logging in to the remote site and getting the page data from secure pages.

Here is the what this class has to offer.

Constructor function has 3 parameters :

First parameter is to allow cookie usage or not , true or false.
Second parameter is to set the time out of connections. Integer value in seconds.
Third parameter is to allocate a filename for the storage of the cookie for this session. This can be usefull when you have to carry the login session of the remote site to multiple scripts in your own code. I generally pass the session id to this parameter , this way if i have to use the remote cookie on next pages as well , i can use it easily. You have to create a folder called “cookies” and set its permission to 0777 for this to work correctly. All cookies will be stored there.

The class has some accessable variables as well. They help you use proxies for your remote requests ;)
var $proxyaddr; //— you can set the proxy ip or host to use
var $proxyport; //– proxy port
var $proxyuser; //– proxy username
var $proxypass; //– proxy password

The getdata function lets you actually request the remote url and get its data. The function has these parameters.

First parameter is the url you want to request.
Second parameter is the post data you want to send to the remote url. For login, search etc.
Third parameter is the custom referrer you want to send.
Fourth parameter is wether to set cookie or not.
Fifth parameter is wether to use the cookie made earlier.
Sixth parameter is to send a custom user agent you want to send for the request.
Seventh parameter is rarely used , sometimes even when you are doing every thing right , you dont get the expected results after sending post data. Setting this parameter to true , sends the post data as string to curl instead of sending it as an array.

Another usefull function in the class is the search function. This helps you get specific regions out of the html. For instance if you want to get all the <p> inside the html , you can do this.

$return_array = $browser->search(”<p” , “</p>” , $data);

The last parameter of the search function lets you remove the start and end strings from the returned results. The result from this function is an array.

This class i developed has made alot of things easier for me over the years . This makes alot of things easier for you when using curl with php , saves you a bundle of time.

Hope this helps someone else as well. :)

Here is the download link

streamload.com aka stressload.com

Dec 4
Posted by : Sabeen Malik in Misc, Web World

Since the switch over to mediamax , streamload sux.

I have received several comments and have experienced the bad service myself as well. So i have been trying to find good alternatives to FTP upload to remote storage. Havent come up with many yet . But i will definitely post here if i do.

Some easy alternatives for people with online storage needs are www.box.net , the amazon S3 service , www.xdrive.com and even the G-Drive hack :)

I guess streamload will be another victim of the capatalist idealogy. People who generally take over good services/comapnies for some odd reason end up screwing them up. Another example i went through myself was www.mesopia.com now www.netbunch.com. The guys at webhostplus.com made sure NO ONE EVER goes the netbunch way again. I hope the FBI case on webhostplus people picks up pace soon.