PHP 使用 fsockopen 获取和发布数据
如何使用 PHP 通过同一个套接字获取和发布数据?我有这样的代码:
$fp = fsockopen("ssl://ovi.rdw.nl", 443, $errno, $errstr, 30);
if(!$fp){
echo $errstr;
}else{
$post_data = 'ctl00$cntMaincol$btnZoeken=Zoeken&ctl00$cntMaincol$txtKenteken=83FHVN';
$out = "GET /Default.aspx HTTP/1.0\r\n";
$out .= "Host: ovi.rdw.nl\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while(!feof($fp)){
$data = fgets($fp);
$view_state = getViewState($data);
if($view_state != ""){
echo $view_state."<br />";
break;
}
}
$post_data = "__VIEWSTATE={$view_state}&".$post_data;
$out = "POST /Default.aspx HTTP/1.0\r\n";
$out .= "Host: ovi.rdw.nl\r\n";
$out .= "Content-type: application/x-www-form-urlencoded\r\n";
$out .= "Content-length: " . strlen($post_data) . "\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
fwrite($fp, $post_data);
while(!feof($fp)){
echo fgets($fp);
}
}
它得到的数据是正确的,但发布它并不顺利。我错过了什么?
How to get, and post data through the same socket with PHP? I have this code:
$fp = fsockopen("ssl://ovi.rdw.nl", 443, $errno, $errstr, 30);
if(!$fp){
echo $errstr;
}else{
$post_data = 'ctl00$cntMaincol$btnZoeken=Zoeken&ctl00$cntMaincol$txtKenteken=83FHVN';
$out = "GET /Default.aspx HTTP/1.0\r\n";
$out .= "Host: ovi.rdw.nl\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while(!feof($fp)){
$data = fgets($fp);
$view_state = getViewState($data);
if($view_state != ""){
echo $view_state."<br />";
break;
}
}
$post_data = "__VIEWSTATE={$view_state}&".$post_data;
$out = "POST /Default.aspx HTTP/1.0\r\n";
$out .= "Host: ovi.rdw.nl\r\n";
$out .= "Content-type: application/x-www-form-urlencoded\r\n";
$out .= "Content-length: " . strlen($post_data) . "\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
fwrite($fp, $post_data);
while(!feof($fp)){
echo fgets($fp);
}
}
It get's the data right, but the posting it is not going ok. What do i mis?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在同一连接中执行 GET 和 POST,这对于您已通过连接指定并重新保证的 HTTP/1.0 无效:关闭。注释掉你的获取部分,然后就可以发帖了。
您可以通过 post 获取数据,因此不需要执行 get 和 post。或者,如果您确实需要执行 get 和 post,请关闭套接字,然后再次为 post 重新建立套接字。
You're doing a GET and a POST in the same connection, This isn't valid for HTTP/1.0 which you have specified and re-assured via connection: close. Comment out your get portion and just do the post.
You can get data back with a post, so you don't need to do a get and a post. Or if you do need to do a get and a post, close the socket, then re-establish the socket again for the post.
不要忘记
fflush()
don't forget to
fflush()
在某些情况下,Curl 太重,无法使用 post_to_host():
发布到托管 php 项目位置: http://code.google.com/p/post-to-host/
Curl is too heavy in some case, to use post_to_host():
post to host php project location: http://code.google.com/p/post-to-host/