此查询重点是使用 Curl 登录 Barnes & Noble 的移动网站,由于使用 SSL 和 cookie 而遇到困难。
要解决该问题,请考虑以下调整:
以下代码将这些调整合并到一个工作示例中:
// Constants $EMAIL = '[email protected]'; $PASSWORD = 'yourpassword'; $COOKIE_FILE = "/tmp/cookies.txt"; $LOGIN_URL = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn"; $AGENT = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)"; // Curl Initialization $ch = curl_init(); // Basic Headers $headers = ["Accept: */*", "Connection: Keep-Alive"]; // 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); curl_setopt($ch, CURLOPT_COOKIEJAR, $COOKIE_FILE); // Step 1: Initialize with Login URL curl_setopt($ch, CURLOPT_URL, $LOGIN_URL); curl_exec($ch); // Step 2: Extract Form Fields $content = curl_getinfo($ch); $fields = getFormFields($content); // Populate Form Fields $fields['emailAddress'] = $EMAIL; $fields['acctPassword'] = $PASSWORD; // Extract X Value $loginUrl = "https://cart2.barnesandnoble.com/mobileacct/op.asp?x={$x}"; // Construct POST Fields $postFields = http_build_query($fields); // Update Login URL and POST Options curl_setopt($ch, CURLOPT_URL, $loginUrl); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); // Perform Login and Retrieve Result $result = curl_exec($ch); print $result;
寻求进一步帮助在使用 Curl 时,请考虑以下额外资源:
以上是如何使用 Curl、Cookie 和 SSL 登录 Barnes & Noble 的移动网站?的详细内容。更多信息请关注PHP中文网其他相关文章!