0

私のサイトは、cURL を使用して取得した別のサイトからスケジュールを取得して投稿します。スケジュールは毎日変更されますが、サーバー上の Cookie ファイルを削除しない限り、スケジュールの最新バージョンがサイトに投稿されないため、Cookie を更新する必要があると思いますが、更新されていません。

追加情報: Cookie ファイルのパーミッションは 644 です。そこにファイルがない場合、cURLがファイルを作成するので、読み書きできると思います。

助けてくれてありがとう!

コード:

<?php
$login_url = 'https://example.com';

//These are the post data username and password
$post_data = 'username=user&password=password&external_login=0&action=login';

//Create a curl object
$ch = curl_init();

//Set the useragent
$agent = $_SERVER["HTTP_USER_AGENT"];
curl_setopt($ch, CURLOPT_USERAGENT, $agent);

//Set the URL
curl_setopt($ch, CURLOPT_URL, $login_url );

//This is a POST query
curl_setopt($ch, CURLOPT_POST, 1 );

curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

//Set the post data
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

//We want the content after the query
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//Follow Location redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

/*
Set the cookie storing files
Cookie files are necessary since we are logging and session data needs to be saved
*/

curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

//Execute the action to login
$postResult = curl_exec($ch);

$geturl='https://example.com/schedule';

curl_setopt($ch, CURLOPT_URL, $geturl);
curl_exec($ch);

if(curl_exec($ch) === false)
{
echo 'Error: ' . curl_error($ch);
}

curl_setopt($ch, CURLOPT_URL, $geturl);

$schedule = curl_exec($ch);

echo $schedule;

curl_close($ch);

?>

Cookie ファイルの内容は次のとおりです。

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

example.com FALSE   /   FALSE   0   code1234    codexxxxxxxxxx
example.com FALSE   /id/    FALSE   12345678    display_mobile_version_1620 0
4

2 に答える 2

1

curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); を追加しました。上記に加えて、現在、スケジュールは最新バージョンをプルしています。

于 2013-11-17T16:05:29.977 に答える
0

CURLOPT_FRESH_CONNECT TRUEを使用して、新しい接続の使用を強制することができます。

curl_setopt($curl1, CURLOPT_FRESH_CONNECT, TRUE);

しかし、あなたのファイルに含まれている Cookie について知りたいです。

于 2013-11-14T15:28:46.697 に答える