PHP - https:// POST 请求?
我的代码错误在哪里?
<?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 未启用。
where's my mistake in the code?
<?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;
?>
I want to make an auto login script for some sites for the private use, this code should perform a redirection to facebook and automaticly log into an account, but the code isn't working, my browser window is still blank.
Would be great if anyone, could help me. I already tried this with CURL, but my test-site Facebook says, that Cookies aren't enabled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是响应是没有正文的标头重定向。
使用我为情况编写的 此类 尝试一下其中 cURL 不可用 - 它完全执行您想要执行的操作,但通过为您处理连接和消息构造以及标头重定向来完成大量工作。还提供有意义的错误消息。记录在文件顶部的注释中。
使用上面的类并尝试以下代码(EDITED FOR COOKIES):
My guess is that the response is a header redirect with no body.
Try it using this class I wrote for situations where cURL is not available - it does exactly what you are trying to do there, but takes a lot of the work out of it by handling the connection and message construction for you, as well as header redirects. Also provides meaningful error messages. Documented in comments at the top of the file.
Use the above class and try the following code (EDITED FOR COOKIES):