File_get_contents接收和发送cookie

时间:2012-07-23 13:50:48

标签: php cookies file-get-contents

我正在尝试使用PHP的函数file_get_contents()为服务器Strava.cz创建非官方API ;但它不起作用。怎么了? :'(

function strava_czLogin($machine,$user,$pass){
//Sends the request
$src = file_get_contents(
            'http://m.strava.cz/Stravnik/formprihlaseni.aspx',
            false,
            stream_context_create(
             array('http'=>array('header' => "User-Agent:(none; StravaCekujNET; Linux x86_64; rv:1.0)\r\n"))
            )
           );
$src = mb_convert_encoding($src,'utf-8','auto'); //Encoding correction

//Works with the cookies (yummy!)
$cookies = array();
foreach ($http_response_header as $value) {
    if (preg_match('/^Set-Cookie:\s*([^;]+)/', $value, $matches)) {
        parse_str($matches[1], $tmp);
        $cookies += $tmp;
    }
}

$sessionId = "";
foreach ($cookies as $key => $value) {
    if ($key == "ASP_NET_SessionId"){$sessionId = $value;}
}

//Checks the result and returns the sessionId
if (preg_match('<form name="aspnetForm" method="post" action="stravnik.aspx" id="aspnetForm">',$src)){
     return array(true,$sessionId);
    }else{
     return array(false,null);
    }
}

function strava_czCheck($sessionId){
    //Sends request
    $src = file_get_contents('http://m.strava.cz/Stravnik/objednavky.aspx',false,stream_context_create(array('http'=>array('header' => "User-Agent:(none; StravaCekujNET; Linux x86_64; rv:1.0)\r\nCookie: ASP_NETSession=".$sessionId."\r\n"))));
    $src = mb_convert_encoding($src,'utf-8','auto'); //Zajistí správné kódování
    echo $src; //Prints the response - "Application Error"

    //Some irrelevant code here


    //Returns the result
    if ($arr==null) {return array(false,null);}
    else {return array(true,$arr);}
}

服务器返回“应用程序错误”,就像您注销时一样 不要问我为什么要这样做 - 我是那个问:D

的人

1 个答案:

答案 0 :(得分:1)

如果您想处理cookies,请使用curl。也许这会有所帮助: https://rupeshpatel.wordpress.com/2012/05/12/general-purpose-function-for-curl/

相关问题