2011-10-08 121 views
0

哪裏是我在代碼中的錯誤?PHP - https:// POST-Request?

<?php 
    error_reporting(-1); 
    function PostRequest($url, $referer, $_data) { 
     // convert variables array to string: 
     $data = array(); 
     while(list($n,$v) = each($_data)){ 
      $data[] = "$n=$v"; 
     } 
     $data = implode('&', $data); 
     // format --> test1=a&test2=b etc. 

     // parse the given URL 
     $url = parse_url($url); 

     // extract host and path: 
     $host = $url['host']; 
     $path = $url['path']; 

     // open a socket connection on port 80 
     $fp = fsockopen("ssl://".$host, 443); 

     // send the request headers: 
     fputs($fp, "POST $path HTTP/1.1\r\n"); 
     fputs($fp, "Host: $host\r\n"); 
     fputs($fp, "Referer: $referer\r\n"); 
     fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 
     fputs($fp, "Content-length: ". strlen($data) ."\r\n"); 
     fputs($fp, "Connection: close\r\n\r\n"); 
     fputs($fp, $data); 

     $result = ''; 
     $safe=0; 
     while(!feof($fp)&&$safe<1000) { 
      // receive the results of the request 
      $result .= fgets($fp, 128); 
      $safe++; 
     } 

     // close the socket connection: 
     fclose($fp); 

     // split the result header from the content 
     $result = explode("\r\n\r\n", $result, 2); 

     $header = isset($result[0]) ? $result[0] : ''; 
     $content = isset($result[1]) ? $result[1] : ''; 

     // return as array: 
     return array($header, $content); 
    } 

    /* 
    ** The example: 
    */ 

    // submit these variables to the server: 
    $data = array(
     'email'=>'testemail', 
     'pass'=>'testpass' 
    ); 

    list($header, $content) = PostRequest("https://login.facebook.com/login.php", "http://facebook.com", $data); 
    // print the result of the whole request: 
    print $content; 
?> 

我想爲一些網站爲私人使用自動登錄腳本,此代碼應進行重定向到Facebook和全自動登錄到一個帳戶,但該代碼不工作,我的瀏覽器窗口仍然空白。

會很棒,如果有人,可以幫助我。我已經用CURL試過了,但我的測試網站Facebook說,Cookies沒有啓用。

+4

使用捲曲。除非您瞭解成功完成HTTPS請求所需的私鑰和公鑰以及握手,否則不要插入HTTPS服務器。 –

+3

你有沒有在你的服務器上配置ssl? –

+1

我試過cURL,但它沒有工作,因爲有這些餅乾:( – Jannik

回答

0

我的猜測是響應是一個沒有內容的頭部重定向。

嘗試使用this class我寫了一些cURL不可用的情況 - 它完全按照你在那裏做的事情做,但是通過爲你處理連接和消息構造來完成其中的許多工作,例如以及頭重定向。還提供有意義的錯誤消息。記錄在文件頂部的註釋中。

使用上面的類和嘗試下面的代碼(編輯的COOKIES):

<?php 

    function process_cookies ($httpobj, $cookiejar = array()) { 
    // Function get the cookies that should be sent in the next request 
    if (!count($cookies = $httpobj->getResponseCookies())) return $cookiejar; 
    foreach ($cookies as $key => $data) { 
     if ($data['value'] !== '' && (!isset($data['expirestime']) || $data['expirestime'] > time())) { 
     $cookiejar[$key] = $data['value']; 
     } else if (isset($cookiejar[$key])) { 
     unset($cookiejar[$key]); 
     } 
    } 
    return $cookiejar; 
    } 

    $url1 = 'http://www.facebook.com/'; 
    $url2 = 'https://login.facebook.com/login.php'; 

    // Fetch the class 
    require('class.httprequest.php'); 

    // Create an object and get the main page of facebook.com 
    $request1 = new httprequest; 
    if (!$request1->setRequestURL($url1)) exit("Error setting request 1 URL: ".$request1->getLastErrorStr()."<br>\r\n"); 
    if (!$request1->sendRequest()) exit("Error sending request 1: ".$request1->getLastErrorStr()."<br>\r\n"); 

    // Create an object for the POST request 
    $request2 = new httprequest; 

    // Set request method and URL  
    $request2->setRequestMethod('POST'); 
    if (!$request2->setRequestURL($lastLocation = $url2)) exit("Error setting request URL: ".$request2->getLastErrorStr()."<br>\r\n"); 

    // All the other headers will be built by the class but we need 
    // set this one explicitly 
    $request2->setRequestHeader('Referer',$url1); 

    // Get the cookies sent by the last request and forward them on 
    $cookiejar = process_cookies($request1); 
    if (count($cookiejar)) $request2->setRequestCookies($cookiejar); 

    // Set the request body 
    $data = array(
    'email'=>'testemail', 
    'pass'=>'testpass' 
); 
    $request2->setRequestControls($data); 

    // We need to handle redirects ourselves to make sure the cookies are sent correctly 
    $request2->setHandleRedirects(FALSE); 

    // Keep looping until we get a 2xx response 
    while (TRUE) { 
    if (!$request2->sendRequest()) exit("Error sending request: ".$request2->getLastErrorStr()."<br>\r\n"); 
    if ($request2->getResponseCode() >= 200 && $request2->getResponseCode() < 300) break; 
    if ($request2->getResponseCode() >= 300 && $request2->getResponseCode() < 400) { 
     $nextLocation = $request2->getRequestHeader('location'); 
     if (!$nextLocation) exit("Error: Server responded with redirect response code ".$request2->getResponseCode().", but did not send a valid location header<br>\r\n"); 
     $cookiejar = process_cookies($request2,$cookiejar); 
     $request2->resetRequestCookies(); 
     $request2->setRequestCookies($cookiejar); 
     $request2->setRequestHeader('Referer',$lastLocation); 
     if (!$request2->setRequestURL($lastLocation = $nextLocation)) exit("Error setting request URL: ".$request2->getLastErrorStr()."<br>\r\n"); 
     continue; 
    } 
    exit("Server responded with unhandled HTTP response code: ".$request2->getResponseCode()."<br>\r\n"); 
    } 

    // validate and display the response 
    if (!$response = $request2->getResponseBodyData(TRUE)) exit('Error: No body data returned<br>'."\r\n"); 
    exit($response); 

?> 
+0

對不起,修復了無效鏈接...:S – DaveRandom

+0

你要麼得到一個標題重定向包含需要發回的cookies,或者期望通過訪問facebook.com(推薦人)收到的cookies。嘗試上面編輯的代碼。 – DaveRandom

+0

修復方法是:將行'$ nextLocation = $ request2-> getRequestHeader('location');'with'$ nextLocation = $ request2-> getResponseHeader('location');'。但是,我剛剛用我的FB賬戶嘗試了這個,它似乎陷入了重定向循環,每個響應都是302,並且它會一直重複指向相同的位置,我會繼續玩它,並讓你知道我是否在任何地方...... – DaveRandom