簡體   English   中英

使用cURL登錄網站

[英]Site login using cURL

我是第一次使用cURL。 我需要登錄網站,我的代碼無法正常工作。 救命。

我在設置餅干時遇到問題

<?php

$cookie_file_path = tempnam('/tmp', 'cookie');

// Login variables
$urls = 'http://mypage.php';
$uname = 'user';
$upass = 'pass';
$unamefield = '*****';
$upassfield = '*****';
$usubmit = 'submit';

// Log into the specified website and return the cURL variable
function login($loginURL, $username, $userFieldName, $password, $passFieldName, $usubmitContent)  {
        // Initialize the page
        $page = curl_init($loginURL);
        // Set POST data
        curl_setopt($page, CURLOPT_POST, 1);
        $postData = $userFieldName . '=' . $username . '&' . $passFieldName . '=' . $password;
        if(!empty($usubmitContent))       $postData = $postData . '&' . $usubmitContent;

        curl_setopt($page, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($page,      CURLOPT_RETURNTRANSFER, true);

        // Set the location of and send the cookies
        curl_setopt($page, CURLOPT_COOKIEJAR, 'cookies.txt');

        // return the cURL variable for later use
        return $page;
}

$page = login($urls, $unamefield, $uname, $upassfield, $upass, $usubmit);
$result = curl_exec($page);

// curl_close($page);

print $result;

$page = curl_init('http://mypage.php');
// Set the location of and send the cookies
curl_setopt($page, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($page, CURLOPT_RETURNTRANSFER, $cookie_file_path);
$result = curl_exec($page);
curl_close($page);

print $result;
?>

我用谷歌搜索它是什么問題,沒有運氣。

根據頂部的Login變量,看起來您的名稱和密碼變量定義不正確。 不應該是:

// Login variables
$urls = 'http://mypage.php';
$unamefield = 'user';
$upassfield = 'pass';
$uname= '*****';
$upass= '*****';
$usubmit = 'submit';

您的代碼在第一次print $result;似乎可以正常工作print $result; 那里剩下的代碼是什么(即第二個curl_init)? 另外,使用cookie文件的絕對路徑。 在登錄功能中,執行以下操作:

$cookie_file = "/tmp/cookies.txt";
curl_setopt($page, CURLOPT_COOKIEJAR, $cookie_file);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM