0

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

4

1 回答 1

1

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

于 2012-07-23T13:56:05.453 回答