Using PHP and CURL to log in to a website
The other day I wanted to automate some downloading from a username and password secured website. I wrote a quick script and it is working like a dream, below is the CURL part of the code that does the logging in and download.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
$username = 'myuser'; $password = 'mypass'; $loginUrl = 'http://www.example.com/login/'; //init curl $ch = curl_init(); //Set the URL to work with curl_setopt($ch, CURLOPT_URL, $loginUrl); // ENABLE HTTP POST curl_setopt($ch, CURLOPT_POST, 1); //Set the post parameters curl_setopt($ch, CURLOPT_POSTFIELDS, 'user='.$username.'&pass='.$password); //Handle cookies for the login curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); //Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL //not to print out the results of its query. //Instead, it will return the results as a string return value //from curl_exec() instead of the usual true/false. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //execute the request (the login) $store = curl_exec($ch); //the login is now done and you can continue to get the //protected content. //set the URL to the protected file curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/protected/download.zip'); //execute the request $content = curl_exec($ch); //save the data to disk file_put_contents('~/download.zip', $content); |
PECL UUID extension on Mac OSX
Today I needed to set up the PECL UUID PHP module on my Mac OSX 10.7 Lion development machine, I needed it for a project that I'm working on.
My first reaction was to fire up terminal and run sudo pecl install uuid. It all looked good for a moment but then came to a grinding halt when 'make' failed.
Here is the error for reference (and for those searching snippets of it):
|
1 2 3 4 5 6 7 8 9 10 11 12 |
cc -I. -I/private/tmp/pear/temp/uuid -DPHP_ATOM_INC -I/private/tmp/pear/temp/pear-build-rootEEpqkn/uuid-1.0.2/include -I/private/tmp/pear/temp/pear-build-rootEEpqkn/uuid-1.0.2/main -I/private/tmp/pear/temp/uuid -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/include -DHAVE_CONFIG_H -g -O2 -c /private/tmp/pear/temp/uuid/uuid.c -fno-common -DPIC -o .libs/uuid.o /private/tmp/pear/temp/uuid/uuid.c: In function ‘zm_startup_uuid’: /private/tmp/pear/temp/uuid/uuid.c:89: error: ‘UUID_TYPE_DCE_TIME’ undeclared (first use in this function) /private/tmp/pear/temp/uuid/uuid.c:89: error: (Each undeclared identifier is reported only once /private/tmp/pear/temp/uuid/uuid.c:89: error: for each function it appears in.) /private/tmp/pear/temp/uuid/uuid.c:90: error: ‘UUID_TYPE_DCE_RANDOM’ undeclared (first use in this function) /private/tmp/pear/temp/uuid/uuid.c: In function ‘zif_uuid_create’: /private/tmp/pear/temp/uuid/uuid.c:168: error: ‘UUID_TYPE_DCE_TIME’ undeclared (first use in this function) /private/tmp/pear/temp/uuid/uuid.c:171: error: ‘UUID_TYPE_DCE_RANDOM’ undeclared (first use in this function) /private/tmp/pear/temp/uuid/uuid.c:181: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘long int’ make: *** [uuid.lo] Error 1 ERROR: `make' failed |
After a bit of googling I found out what the problem was and a solution to it. The full explanation and solution can be found here:
http://unrealexpectations.com/blog/2010/04/mamp-pecluuid-module-working-on-snow-leopard/
I can confirm that the patch and build worked on my set up (OSX 10.7 Loin) and I hope by spreading the word it some more people out.
Installing and setting up PEAR
This post will help you get PEAR installed and up and running on your machine, there are two options to below, one describes an OXS Lion specific setup and the other is more generic.
Installing on OSX 10.7 Lion
OSX Lion already has the PEAR install bundle so there is no need to download it, open up a terminal window and enter the following commands:
|
1 2 |
cd /usr/lib/php sudo php install-pear-nozlib.phar |
Downloading and installing
Do not follow this part if you have just done the above steps. Otherwise enter the below into a terminal window:
|
1 2 |
curl http://pear.php.net/go-pear.phar > go-pear.php sudo php -q go-pear.php |
It will ask you to enter a new installation base ($prefix). I recommend you use /usr/local
Configuring PHP include path
Edit /etc/php.ini and find the line: ;include_path = ".:/php/includes". Uncomment it (remove the ; at the beginning) and add the PEAR path to it. It should end up looking similar to the below:
|
1 |
include_path = ".:/usr/lib/php/pear" |
Update PEAR
Now to update the install enter the 3 commands below into the terminal window:
|
1 2 3 |
sudo pear channel-update pear.php.net sudo pecl channel-update pecl.php.net sudo pear upgrade-all |
Install PHPUnit
Just for good measure you will probably also want to install PHPUnit, if you do then execute the below command and your ready to go.
|
1 2 3 4 |
sudo pear channel-discover pear.phpunit.de sudo pear channel-discover pear.symfony-project.com sudo pear install pear.phpunit.de/PHPUnit sudo pear install phpunit/DbUnit --alldeps |
