3

I've made myself a test project to log in to Amazon using cURL and PHP, but after hours of going in circles, I think I have to admit defeat. I was wondering if anyone could tell me were I've gone wrong with the code below?

Besides that, I also downloaded my Amazon cookies and placed them in the same directory as the PHP in a file called 'cookie.txt'

$username ="";      // needs to be changed 
$password = "";     // needs to be changed 

$url = "";          // sign in url  
$cookie = "cookie.txt"; 

$postdata = "email=".$username."&password=".$password; 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch); 

echo $result;  
curl_close($ch);
Expedito
  • 7,771
  • 5
  • 30
  • 43
sam
  • 9,486
  • 36
  • 109
  • 160
  • 2
    Does `curl_error()` return an error? – antoyo May 09 '13 at 15:51
  • Nope, i didn't get any errors back when i placed that at the bottom of my script, just before the last 2 lines. – sam May 09 '13 at 15:53
  • What is the actual problem? An error? Or your login procedure is not working? – CodeZombie May 09 '13 at 16:04
  • @ZombieHunter - When i run the script instead of login me in it just get the login page returned – sam May 09 '13 at 16:05
  • @sam: So there is something wrong with your login and amazon returns you the login page again. Is it the right URL? – CodeZombie May 09 '13 at 16:07
  • having had a poke around i think it might be the latter, ive got through the form and the action= is to a different url, than the page, in that case which one would i be using ? – sam May 09 '13 at 16:13
  • @sam: The one of the form/@action as this is the URL you post your username and password. – CodeZombie May 09 '13 at 16:16
  • and for the $postdata field i need the name attribute of the form inputs, right ? – sam May 09 '13 at 16:27

2 Answers2

0

Do you still want to do this, after amazon has come up with their solution for this, check out - https://login.amazon.com/

nuhbye
  • 89
  • 1
  • 5
  • thanks, thats a link to their api, the question was more an exercise in using CURL, than login into amazon in particular – sam Jun 07 '13 at 10:12
-1

Does this solution work for you?

PHP Curl - Cookies problem

You might adjust the login URL to the actual login URL of amazon.

Community
  • 1
  • 1
g000ze
  • 290
  • 2
  • 11