简体   繁体   English

带有GET / JSON问题的PHP CURL登录表单

[英]PHP CURL LOGIN FORM with GET/JSON Trouble

I'm trying to login into a website using php and curl. 我正在尝试使用php和curl登录网站。 I am able to login and set cookies however I need to make a GET request to a link and the header should look like this. 我可以登录并设置cookie,但是我需要向链接发出GET请求,并且标题应如下所示。 Taken from HTTPLIVEHEADERS (FF). 取自HTTPLIVEHEADERS(FF)。

HEADER: 标题:

http://www.home.com/iframe/fut/p/ut/shards?_=1380785844876

GET /iframe/fut/p/ut/shards?_=1380785844876 HTTP/1.1
Host: www.home.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; rv:24.0) Gecko/20100101 Firefox/24.0
Accept: application/json, text/javascript;
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/json
Session-Data-Nucleus-Id: 211222911
X-UT-Embed-Error: true
X-UT-Route: https://host2.com
X-Requested-With: XMLHttpRequest
Referer: http://www.home.com/iframe/fut/
Cookie: futweb=vk9b5ersdt0sbksooc7p8cj9k7; WEB-SESSION=vev11q6co9nlko4raeisfiqtu5;     hl=us; XSRF-TOKEN=5372d286aa1353e79c68b3d177192e5af7f96fc4; device_view=not_mobile; s_sivo=US%3AEASFW%3AFUT; s_cc=true; s_ria=flash%20not%20detected%7Csilverlight%20not%20detected; s_pv=NA%3AUS%3ASPORTS%3AEAC%3AONLINEGAME%3AFIFA%3AEASFW%3AFUT%3ALANDINGPAGE; s_nr1=1380785843058-NEW; s_sq=%5B%5BB%5D%5D; utag_main=_st:1380787644943$ses_id:1380786810666%3Bexp-session; s_ppv=100
Connection: keep-alive

i have done similar task like this 我已经完成了类似的任务

        $ckfile = tempnam(APPPATH. "/temp", "CURLCOOKIE");
        remote_login($ckfile);
        uploadData($someDataTobePosted,$ckfile);

then here is function for login. 然后是登录功能。

    private function remote_login($ckfile) {
    $username = USER;
    $password = PWD;
    $url = "https://api.safecast.org/en-US/users/sign_in";
    $postdata = "user[email]=" . $username . "&user[password]=" . $password;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($ch, CURLOPT_POST, 1);
    $result = curl_exec($ch);
    $this->log_cron_response($result);
    curl_close($ch);
}

then another fun for posting or geting data 然后发布或获取数据的另一个乐趣

    private function uploadData($data,$ckfile){
    $ch = curl_init("https://api.safecast.org/measurements.json");
    $postdata = 
            "measurement[captured_at]=" . date("Y/m/d H:i:s",$data['radiation_time']/1000) 
            ."&measurement[device_id]=" . SAFE_CAST_DEVICE_ID
            ."&measurement[unit]=" . SAFE_CAST_UNIT
            ."&measurement[user_id]=" . SAFE_CAST_USER_ID
            ."&measurement[value]=" . $data['radiation_value']
            ."&measurement[latitude]=" . $data['lat']
            ."&measurement[longitude]=" . $data['long'];
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
    curl_setopt($ch, CURLOPT_REFERER, "https://api.safecast.org/measurements.json");
    $output = curl_exec($ch);
    $this->log_cron_response($output);
}

you can use it $ckfile = tempnam(APPPATH. "/temp", "CURLCOOKIE"); 您可以使用它$ ckfile = tempnam(APPPATH。“ / temp”,“ CURLCOOKIE”); remote_login($ckfile); remote_login($ ckfile); $this->uploadData($value,$ckfile); $ this-> uploadData($ value,$ ckfile);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM