如何使用Curl、SSL 和Cookie 登入
嘗試使用Curl、SSL 和cookie 登入barnesandnoble.com 時,有些使用者由於網站協議的差異和潛在的cookie 處理問題而遇到困難。以下是問題的細分與解決方案:
問題:
解決方案:
要使用Curl 成功登入barnesandnoble.com,請考慮以下內容:
// Options $EMAIL = '[email protected]'; $PASSWORD = 'yourpassword'; $cookie_file_path = "/tmp/cookies.txt"; $LOGINURL = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn"; $agent = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)"; // Begin Script $ch = curl_init(); // Extra Headers $headers[] = "Accept: */*"; $headers[] = "Connection: Keep-Alive"; // Basic Curl Options curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); // Initial URL curl_setopt($ch, CURLOPT_URL, $LOGINURL); // Get Cookies and Form Inputs $content = curl_exec($ch); // Extract Hidden Inputs $fields = getFormFields($content); $fields['emailAddress'] = $EMAIL; $fields['acctPassword'] = $PASSWORD; // Get x Value $x = ''; if (preg_match('/op\.asp\?x=(\d+)/i', $content, $match)) { $x = $match[1]; } // Updated Login URL $LOGINURL = "https://cart2.barnesandnoble.com/mobileacct/op.asp?x=$x"; // Post Options curl_setopt($ch, CURLOPT_URL, $LOGINURL); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); // Perform Login $result = curl_exec($ch); print $result; // Helper Function to Extract Form Inputs function getFormFields($data) { $inputs = array(); $elements = preg_match_all('/(<input[^>]+>)/is', $data, $matches); if ($elements > 0) { for ($i = 0; $i < $elements; $i++) { $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]); if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) { $name = $name[1]; $value = ''; if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) { $value = $value[1]; } $inputs[$name] = $value; } } } return $inputs; }
以上是為什麼使用 Curl、SSL 和 cookie 登入 Barnes & Noble 的行動網站很困難?的詳細內容。更多資訊請關注PHP中文網其他相關文章!