您的位置:首页 > 其它

调接口发送短信 :用file_get_contents函数,以post方式获取url

2014-12-17 09:08 507 查看
例:

<?php
$data=array('foo'=>'bar');
$data=http_build_query($data);

$opts=array(
'http'=>array(
'method'=>'POST',
'header'=>"Content-type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($data)."\r\n",
'content'=>$data
),
);
$context=stream_context_create($opts);
$html=file_get_contents('http://localhost/e/admin/test.html',false,$context);
echo$html;
?>


应用:post方法调接口

//----------------发送短信--------------------------------
//----------------发送短信--------------------------------
public function sendMessage($mobile,$message,$teamid){
//--2014年12月15日---
//email=hrb&Password=0827&MsgContent=您在xxx网站注册的手机验证码为:
//yvp4vq,此验证码同时也是您的初始登录密码,请在登录后及时修改。
//&mobileNumber=18710000000&SendTime=&SubNumber=
$sql = "insert into message (contect,sendtime,mobile,teamid) values
('".$message."','".date('Y-m-d H:i:s')."','".$mobile."',".$teamid.")";
//var_dump($sql);
$query = $this->db->simple_query($sql);
if($query){
$insID = $this->db->insert_id();
date_default_timezone_set('Asia/Chongqing');
$data=array('email'=>'XXX',  //密码
'Password'=>'XXX ', //帐号
'MsgContent'=>$message,//'hrb'.date('Y-m-d H:i:s'),
'mobileNumber'=>$mobile,
'SendTime'=>date('Y-m-d H:i:s'),
'SubNumber'=>'',);
//echo date('y-m-d h:i:s',time());

$data=http_build_query($data);
$opts=array(
'http'=>array(
'method'=>'POST',
'header'=>"Content-type:application/x-www-form-urlencoded\r\n".
"Content-Length:".strlen($data)."\r\n",
'content'=>$data
),
);
$context=stream_context_create($opts);
$html = file_get_contents('http://www.xxxx.com.cn/api/send.asp',false,$context);//http接口地址
//echo $html;
$result = strpos($html,'<string xmlns="http://tempuri.org/">0');//调用接口时返回的值,右击网页源代码可看到。为这个时代表发送成功
//var_dump($result);
if($result){
$sql = "update message set status = 1 where id = ".$insID;
$query = $this->db->simple_query($sql);
//echo "短信发送成功!";
return TRUE;
}else{
return FALSE;
//echo "短信发送失败!";
}
}
}


转自:http://www.jb51.net/article/20705.htm

另外 :http://bbs.csdn.net/topics/370026897 调用接口 (get方法),

$phonenumber = base64_encode('18999999999');
$url = '/interface/11889/getAppListByPhone.php&phonenumber=' . $phonenumber;

//发送选项
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>15,
)
);

//发送
$context = @stream_context_create($opts);
$result = @file_get_contents($url,false,$context);
$obj = json_decode($result);  // 若json_decode($result,TRUE); 则转为数组,下边的判断就为$ojb['status']
if(isset($obj) && $obj->status)
echo 'OK';
else
echo 'FALSE';


//  <span style="color:#006600;font-size:16px">json_encode()只将索引数组(indexed array)转为数组格式,而将关联数组(associative array)转为对象格式。</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐