通过curl形成登录和发布参数

时间:2019-05-12 12:38:27

标签: php curl

我需要登录R-studio才能捕获(卷曲)图像。 我使用以下过程,但是它不返回图像,仅返回表单本身。 这是表格的位置: http://skiweather.eu:8787/auth-sign-in

    <?php
$username = 'admin';
$password = 'XXX';
$loginUrl = 'http://skiweather.eu:8787/auth-do-sign-in';
$sign=1;
$cookie= "cookies.txt";

//init curl
$ch = curl_init();

//Handle cookies for the login
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)

//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $loginUrl);

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


$response = curl_exec($ch);
if (curl_errno($ch)) die(curl_error($ch));
$dom = new DomDocument();
$dom->loadHTML($response);
$tokens = $dom->getElementsByTagName("meta");
for ($i = 0; $i < $tokens->length; $i++)
{
    $meta = $tokens->item($i);
    if($meta->getAttribute('name') == 'csrf-token')
    $token = $meta->getAttribute('content');
}

$postinfo = 'username='.$username.'&password='.$password.'&staySignedIn=1&appUri='.$loginUrl.'&_csrf='.$token;
echo $token; //debug info
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);


//set the URL to the protected file
curl_setopt($ch, CURLOPT_URL, 'http://skiweather.eu:8787/files/R/devel/PLOTS/snowalert_today_0.png');

//execute the request
$content = curl_exec($ch);

curl_close($ch);

//save the data to disk
file_put_contents('pppp.png', $content);

?>

0 个答案:

没有答案