【发布时间】: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