Archive for the ‘Programming’ Category

php + curl = convenient

Monday, July 17th, 2006

There nothing major or significant going on in my life and therefore I did not bother writing anything down, for quite a while, as you might know. Anyway, today I'm writing a little utility to help fetching images on remote server and store it into sequential names, all within PHP.

To initiate a curl request, you declare one like this:

$c = curl_init("http://server/page.html?getopt=value");

Then set options for this curl request:

curl_setopt($c, CURLOPT_RETURNTRANSFER,1); // stops the request from outputting immediately

curl_setopt($c, CURLOPT_USERAGENT, " Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"); // use a very legit fake useragent

curl_setopt($c, CURLOPT_REFERER, "http://server"); // bypassing measures preventing hotlinking/stealing/whatever u'd like to call it

A complete listing of all available curl options is here. After the options are set, execute the request and store the result like this:

$result = curl_exec($c);

$curlinfo = curl_getinfo($c);

$httperrorcode = $info['http_code'];

If the query is proper, the $httperrorcode should be 200. Assuming you've successfully (200) obtained the targeted document, these are helpful methods to use to manipulate/store the data:

  • strlen( $var )
  • str_replace( "replace this with", "this", $from_this )
  • explode( "seperator", $turn_into_array )
  • sizeof( $var )
  • strstr( $does_this_contain, "this" )
  • print( $var )
  • echo( $var )
  • mkdir( "ABSPATH", 0777)
  • $fp = fopen( "ABSFILENAME", 'w' );
  • fwrite( $fp, $result )
  • fclose( $fp )

 

 

After the whatever-purpose php script is done, simply use the following way to execute under console:

php filename.php getopt1=val1 getopt2=val2

Of course, you access those val1 and val2 within the program with $_GET['getopt1'] and etc. 

old vb project

Tuesday, August 2nd, 2005

http://www.tarotoast.com/www/project/

All Rights Reserved Copyright © 2008 Design by StyleShout and Clazh