線上估價,比價最容易
給你最便宜的價格,消費不吃虧
平價搬家公司優質裝潢設計裝潢團隊,空間規劃
平價奢華設計,免費設計諮詢

首頁  •  tw 論壇 • 程式設計討論     • 

[php] CURL post and cookie example

房東:jeff
發表時間:2012-07-18
[檢舉]


/*
  Here is a script that is usefull to :
  - login to a POST form,
  - store a session cookie,
  - download a file once logged in.
*/

// INIT CURL
$ch = curl_init();

// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, \'
http://www.external-site.com/Members/Login.php\');

// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, \'fieldname1=fieldvalue1&fieldname2=fieldvalue2\');

// IMITATE CLASSIC BROWSER\'S BEHAVIOUR : HANDLE COOKIES
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 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);

// SET FILE TO DOWNLOAD
curl_setopt($ch, CURLOPT_URL, \'
http://www.external-site.com/Members/Downloads/AnnualReport.pdf\');

// EXECUTE 2nd REQUEST (FILE DOWNLOAD)
$content = curl_exec ($ch);

// CLOSE CURL
curl_close ($ch);

/*
  At this point you can do do whatever you want
  with the downloaded file stored in $content :
  display it, save it as file, and so on.
*/




  • 贊助網站       

    廣利不動產-新板特區指名度最高、值得您信賴的好房仲
    您的托付,廣利用心為您服務
    廣利不動產-板橋在地生根最實在--新板特區指名度最高、值得您信賴的好房仲
    完整房訊,房屋、店面熱門精選物件,廣利不動產 優質仲介,房屋租賃、買賣資訊透明,交易真安心!

  • 1 樓住戶:jeff
    發表時間:2012-07-18
    [檢舉]






    # Define target page
    $target = "http://www.schrenk.com/nostarch/webbots/cookie_authentication/index.php";

    # Define the login form data
    $form_data="enter=Enter&username=webbot&password=sp1der3";

    # Create the cURL session
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $target); // Define target site
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Return page in string
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); // Tell cURL where to write cookies
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt"); // Tell cURL which cookies to send
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $form_data);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects

    # Execute the PHP/CURL session and echo the downloaded page
    $page = curl_exec($ch);
    echo $page;

    # Close the cURL session
    curl_close($ch);
    ?>

    Query Session 認證






    # Define target page
    $target = "http://www.schrenk.com/nostarch/webbots/basic_authentication/index.php";

    # Define login credentials for this page
    $credentials = "webbot:sp1der3";

    # Create the cURL session
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $target); // Define target site
    curl_setopt($ch, CURLOPT_USERPWD, $credentials); // Send credentials
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Return page in string

    # Echo page
    $page = curl_exec($ch); // Place web page into a string
    echo $page; // Echo downloaded page

    # Close the cURL session
    curl_close($ch);
    ?>









    # Include libraries
    include("LIB_http.php");
    include("LIB_parse.php");

    # Request the login page
    $domain = "http://www.schrenk.com/";
    $target = $domain."nostarch/webbots/query_authentication";
    $page_array = http_get($target, $ref="");
    echo $page_array['FILE']; // Display the login page
    sleep(2); // Include small delay between page fetches
    echo "
    ";

    # Send the query authentication form
    $login = $domain."nostarch/webbots/query_authentication/index.php";
    $data_array['enter'] = "Enter";
    $data_array['username'] = "webbot";
    $data_array['password'] = "sp1der3";
    $page_array = http_post_form($login, $ref=$target, $data_array);
    echo $page_array['FILE']; // Display first page after login page
    sleep(2); // Include small delay between page fetches
    echo "
    ";

    # Parse session variable
    $session = return_between($page_array['FILE'], "session=", "\"", EXCL);

    # Request subsequent pages using the session variable
    $page2 = $target . "/index2.php?session=".$session;
    $page_array = http_get($page2, $ref="");
    echo $page_array['FILE']; // Display page two
    ?>



    2 樓住戶:阿凱
    發表時間:2012-07-19
    [檢舉]

    /* STEP 1. let’s create a cookie file */
    $ckfile = tempnam ("/tmp", "CURLCOOKIE");

    /* STEP 2. visit the homepage to set the cookie properly */
    $ch = curl_init ("http://somedomain.com/");
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec ($ch);

    /* STEP 3. visit cookiepage.php */
    $ch = curl_init ("http://somedomain.com/cookiepage.php");
    curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec ($ch);

    /* here you can do whatever you want with $output */
    ?>





     共 2 人回應  選擇頁數 【第1 頁】 

    姓名:
    佈告內容: