+ -
当前位置:首页 → 问答吧 → php模拟post发送数据问题

php模拟post发送数据问题

时间:2011-09-28

来源:互联网

function sendSMS($uid,$pwd,$mobile,$content,$time='',$mid='')
{
('15821162098').'&content='.urlencode($content);
$URL = 'http://www.smsadmin.cn/..../'; //需要提交到的页面
//下面这段是要提交的数据
$post_data['uid'] = 'le***';
$post_data['pwd'] = '11111';
$post_data['mobile'] = '158211****';
$post_data['content'] ='您好';
$referrer="";
$URL_Info=parse_url($URL);
if($referrer=="")
{
$referrer=$_SERVER["SCRIPT_URI"];
}
foreach ($post_data as $key=>$value)
{
$values[]="$key=".urlencode($value);
}

$data_string=implode("&",$values);//提交的数据格式是 a=1&b=2 
// Find out which port is needed - if not given use standard (=80)
if (!isset($URL_Info["port"])) {
$URL_Info["port"]=80;
// building POST-request:
//一般做网站用form提交数据后,之后的操作就不用我们不管了,
//这里就是在模拟POST操作的HTTP头,具体的可以去查HTTP协议的资料,并不复杂
$request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
$request.="Host: ".$URL_Info["host"]."\n";
$request.="Referer: $referrer\n";
$request.="Content-type: application/x-www-form-urlencoded\n";
$request.="Content-length: ".strlen($data_string)."\n";
$request.="Connection: close\n";
$request.="\n";
$request.=$data_string."\n";
//exit;
}
//fsockopen的用法就这样子了,不做多说明
$fp = fsockopen($URL_Info["host"], $URL_Info["port"]);
fputs($fp, $request);//把HTTP头发送出去
while(!feof($fp)) {
//$result 是提交后返回的数据
$result .= fgets($fp, 1024);
}
fclose($fp);
//POST方式提交
if( trim($result) == '0发送成功!' )
{
  return "发送成功!";
  }
else 
{
  return "发送失败! 状态:".$result;
}
}
这是一个PHP通过post发送数据到另一个网站实现发送短信的功能,对方采用的是GBK的编码,我们是UTF8的编码,发送过去就出错,返回了一个乱码,前面带了一个5,这个5表示提交信息不全,不清楚哪里出了问题

作者: wts223   发布时间: 2011-09-28

PHP code

function sendSMS($uid,$pwd,$mobile,$content,$time='',$mid='')
{
('15821162098').'&content='.urlencode($content);
$URL = 'http://www.smsadmin.cn/..../'; //需要提交到的页面
//下面这段是要提交的数据
$post_data['uid'] = 'le***';
$post_data['pwd'] = '11111';
$post_data['mobile'] = '158211****';
$post_data['content'] ='您好';
$referrer="";
$URL_Info=parse_url($URL);
if($referrer=="")
{
$referrer=$_SERVER["SCRIPT_URI"];
}
foreach ($post_data as $key=>$value)
{
$values[]="$key=".urlencode($value);
}

$data_string=implode("&",$values);//提交的数据格式是 a=1&b=2 
// Find out which port is needed - if not given use standard (=80)
if (!isset($URL_Info["port"])) {
$URL_Info["port"]=80;
// building POST-request:
//一般做网站用form提交数据后,之后的操作就不用我们不管了,
//这里就是在模拟POST操作的HTTP头,具体的可以去查HTTP协议的资料,并不复杂
$request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
$request.="Host: ".$URL_Info["host"]."\n";
$request.="Referer: $referrer\n";
$request.="Content-type: application/x-www-form-urlencoded\n";
$request.="Content-length: ".strlen($data_string)."\n";
$request.="Connection: close\n";
$request.="\n";
$request.=$data_string."\n";
//exit;
}
//fsockopen的用法就这样子了,不做多说明
$fp = fsockopen($URL_Info["host"], $URL_Info["port"]);
fputs($fp, $request);//把HTTP头发送出去
while(!feof($fp)) {
//$result 是提交后返回的数据
$result .= fgets($fp, 1024);
}
fclose($fp);
            //POST方式提交
    if( trim($result) == '0发送成功!' )
    {
      return "发送成功!";
    }
    else 
    {
      return "发送失败! 状态:".$result;
    }
}
这是一个PHP通过post发送数据到另一个网站实现发送短信的功能,对方采用的是GBK的编码,我们是UTF8的编码,发送过去就出错,返回了



作者: wts223   发布时间: 2011-09-28

如果确定程序没有问题的话,可能就是编码造成的问题。如果是提供的是UTF-8 需要 让对方iconv 进行转码。然后把换回过来的结果在转成UTF-8 给你。

作者: shengli881026   发布时间: 2011-09-28

urlencode json_encode unicode 等方式改成通用的再发
字符串用urlencode
数组用json_encode

作者: yhkyo   发布时间: 2011-09-28

编码不一致且没有转码的话,会导致信息不全的。你们这边如果用GBK的话,发送会正常么?测试过么?

作者: ohmygirl   发布时间: 2011-09-28

没细看你的代码。。返回内容,你转换成UTF-8应该能显示

你可以试着发送仅包含【半角数字+字母】的内容,,

如果还是失败,比对对方的文档和你发送程序的数据,内容

如果成功了,注意内容编码。所有提交的数据,包括短信内容、用户名、密码都转换成GBK格式发送

作者: amani11   发布时间: 2011-09-28

或谁能提供一个模拟post提交数据的函数过来,测了半天也不行

作者: wts223   发布时间: 2011-09-28

引用 6 楼 wts223 的回复:

或谁能提供一个模拟post提交数据的函数过来,测了半天也不行


什么叫模拟POST提交数据函数?自己弄个表单 POST 一下 然后 $_POST 变量数组不就有了?

作者: PhpNewnew   发布时间: 2011-09-28

引用楼主 wts223 的回复:
对方采用的是GBK的编码,我们是UTF8的编码,发送过去就出错
真是天大的笑话!
你的发送,难道不要符合对方的要求吗?

作者: xuzuning   发布时间: 2011-09-28

相关阅读 更多