【问题标题】:PHP - https:// POST-Request?PHP - https:// POST 请求?
【发布时间】:2011-10-08 21:03:03
【问题描述】:

我在代码中的错误在哪里?

<?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 说,没有启用 Cookie。

【问题讨论】:

  • 使用 cURL。除非您了解成功完成 HTTPS 请求所需的私钥和公钥以及握手,否则不要连接到 HTTPS 服务器。
  • 您的服务器上是否配置了 ssl?
  • 我尝试了 cURL,但由于 cookie 的原因,它不起作用 :(
  • 请注意,这很可能会阻止您的 Facebook 帐户。

标签: php facebook post https request


【解决方案1】:

我的猜测是响应是一个没有正文的标头重定向。

使用 this class 尝试它,我为 cURL 不可用的情况编写了它 - 它完全符合您在那儿尝试做的事情,但通过为您处理连接和消息构造来完成大量工作,以及标头重定向。还提供有意义的错误消息。记录在文件顶部的 cmets 中。

使用上述类并尝试以下代码(EDITED FOR 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);

?>

【讨论】:

  • 抱歉,修复了断开的链接...:S
  • 您要么获得包含需要发回的 cookie 的标头重定向,要么期望发送的 cookie 应该通过访问 facebook.com(引荐来源网址)收到。试试上面编辑的代码。
  • 修复是:将$nextLocation = $request2-&gt;getRequestHeader('location');行替换为$nextLocation = $request2-&gt;getResponseHeader('location');。但是,我刚刚用我的 FB 帐户尝试了这个,它似乎陷入了重定向循环,每个响应都是 302,并且它一遍又一遍地重定向到同一个位置。我会继续玩它,如果我到达任何地方,我会告诉你......
  • 目前还没有,但老实说,过去几天我工作很忙,没有玩太多。今晚我可能有机会玩这个 - 如果我想出一个解决方案,我会告诉你的。您到底想做什么 - 即为什么要进行此自动登录,以及您希望通过它实现什么?可能有更好的方法,我在想的只是......
  • 谢谢,慢慢来 :) 如果我能以其他方式与您联系,我可以告诉您更多关于我的项目的信息。登录脚本有点愚蠢,但我没有其他解决方案,所以我需要像这样处理它...... ICQ 或类似的东西?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-09
  • 2013-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多