|
|
|
|
Like this code?
Show the author your appreciation.
|
|
| |
This PHP file gives you a demo for logging into Amazon.com with PHP and CURL.
(http://jgeewax.wordpress.com/2007/01/03/using-php-curl-to-sign-in-to-amazoncom/)
I ran into a lot of trouble trying to do this, so I decided to share what I did.
If you don’t have cURL then this will not work. You need to be able to use the following functions:
|
* curl_init()
* curl_setopt();
* curl_exec();
* curl_close(); | |
The following information is pretty important (and specific to Amazon.com) (remember, this could change at any time and make this code not work.) The idea is that we are emulating the exact same webform that a browser would submit when an actual user is loggin in. The pieces of the form are the following:
| 1. path = gp/yourstore
2. useRedirectOnSuccess = 1
3. query = signIn=1
4. mode =
5. redirectProtocol =
6. pageAction = gp/yourstore
7. disableCorpSignUp =
8. action = sign-in
9. email = (your email here)
10. password = (your password here) | |
This can either be combined together to a string (like email=jjg@go.com&password=mypass&….) or as an array. CURL handles both just fine, and because it is “prettier” to have an array, that is the form I chose.
Here is the first part of the code:
| <?php
$email = ’sample@email.com’;
$password = ‘mypass’;
$data = array(
‘path’ => ‘gp/yourstore’,
‘useRedirectOnSuccess’ => ‘1′,
‘query’ => ’signIn=1′,
‘mode’ => ”,
‘redirectProtocol’ => ”,
‘pageAction’ => ‘gp/yourstore’,
‘disableCorpSignUp’ => ”,
‘action’ => ’sign-in’,
‘email’ => $email,
‘password’ => $password
);
?> | |
After this, we need to actually do the submitting. Logging in is really just authenticating to the server and receiving a cookie for storage on your computer. So we submit the form, and tell CURL to store a cookie in a file we choose. Then, if we tell CURL to read from that cookie file when accessing Amazon.com later, it will act as though we’ve logged in.
Here is the code for submitting with comments:
| <?php
// Initialize the curl object
$cr = curl_init(’https://www.amazon.com/gp/flex/sign-in/select.html’);
curl_setopt($cr, CURLOPT_RETURNTRANSFER, true); // Get returned value as string (don’t put to screen)
curl_setopt($cr, CURLOPT_USERAGENT, $_SERVER[’HTTP_USER_AGENT’]); // Spoof the user-agent to be the browser that the user is on (and accessing the php script)
curl_setopt($cr, CURLOPT_COOKIEJAR, ‘cookie.txt’); // Use cookie.txt for STORING cookies
curl_setopt($cr, CURLOPT_POST, true); // Tell curl that we are posting data
curl_setopt($cr, CURLOPT_POSTFIELDS, $data); // Post the data in the array above
$output = curl_exec($cr); // Execute!
echo $output; // Spit out what we found
curl_close($cr); // Free the memory
?> | |
If it worked successfully, then the following code (which just accesses Amazon’s homepage) will access the homepage of a logged in user. (It should say your name, etc)
| <?php
$cr = curl_init(’http://www.amazon.com/gp/homepage.html’);
curl_setopt($cr, CURLOPT_RETURNTRANSFER, true); // Get returned value as string (don’t put to screen)
curl_setopt($cr, CURLOPT_USERAGENT, $_SERVER[’HTTP_USER_AGENT’]); // Spoof the user agent
curl_setopt($cr, CURLOPT_COOKIEFILE, ‘cookie.txt’); // Use cookie.txt for READING cookies
$output = curl_exec($cr);
curl_close($cr);
echo $output;
?> | |
If everything has gone to plan, then you will have logged in and the cookie (in cookie.txt) will give you access as if you had logged in normally on Amazon.com
I hope this helps someone who is stuck with Amazon! It gave me a bit of troule, but I worked it out eventually. |
|
| A function to check if a URL exists Categories : PHP, CURL, HTTP | | | Returns Yahoo! Address Book and Messenger List as an Array Categories : PHP, PHP Classes, CURL | | | Using cURL to download a file programmatically. Categories : PHP, PHP Extensions, CURL | | | Link Manager for Link Exchangers Categories : PHP, PHP Classes, Databases, MySQL, CURL | | | Sample AIM (Advanced Integration Method) PHP Script For Authorize.net using CURL Categories : CURL, Ecommerce, PHP | | | Query2Report : Generating Html, Pdf and Csv Reports from SQL Query Categories : PHP, PHP, HTML, PDF, Excel | | | Open remote https url using Curl and accept cookie Categories : PHP, CURL | | | Amazon.com API, CURL-REST Parser. Obtain data about Amazon products (PHP5 +) Categories : PHP, Ecommerce, XML, Web Services, CURL | | | These PHP Classes Check if a host is alive using various methods. Categories : PHP, PHP Classes, Sockets, CURL | | | Import the yahoo address book. Categories : PHP, CURL, Authentication | | | Url To Pdf Report By Remote Application Categories : PHP, PHP Classes, PDF, CURL | | | Using data from a string. Categories : PHP, Strings, CURL | | | Yahoo! Messenger Friend List Categories : PHP, CURL, HTML and PHP | | | Newbie Notes #8 - A cron trick Categories : PHP, CURL, Beginner Guides | | | curl_close -- Close a CURL session Categories : PHP, PHP Functions, CURL | |
|
|