PHP Curl - Cookies problem

前端 未结 2 501
说谎
说谎 2020-11-27 17:53

I am trying to grab my amazon associates stats automatically via cUrl. However I am falling down at the first hurdle; logging in.

When I use the following code:

2条回答
  •  感情败类
    2020-11-27 18:04

    You need to get amazon to set the cookie first.

    Try:

    // 1. Create a cookie file and set basic params
    $ckfile = tempnam ("/your/path/to/cookie/folder", "cookie.txt");
    $target_host = "https://affiliate-program.amazon.com";
    $target_request = "/gp/flex/sign-in/select.html";
    $post_data = "action=sign-in&email=$username&password=$password";
    
    // 2. Visit homepage to set cookie
    $ch = curl_init ($target_host);
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec ($ch);
    
    // 3. Continue
    $login = curl_init ($target_host.$target_request);
    curl_setopt($login, CURLOPT_COOKIESESSION, 1);
    curl_setopt($login, CURLOPT_COOKIEJAR, $ckfile);
    curl_setopt($login, CURLOPT_COOKIEFILE, $ckfile);
    curl_setopt($login, CURLOPT_TIMEOUT, 40);
    curl_setopt($login, CURLOPT_RETURNTRANSFER, 1);        
    curl_setopt($login, CURLOPT_HEADER, 1);        
    curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($login, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($login, CURLOPT_POST, 1);
    curl_setopt($login, CURLOPT_POSTFIELDS, $post_data);
    echo curl_exec($login);
    curl_close($login);  
    

提交回复
热议问题