File_get_contents接收和发送cookies

问题描述 投票:0回答:1

我正在尝试使用 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

php cookies file-get-contents
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.