调接口发送短信 :用file_get_contents函数,以post方式获取url
2014-12-17 09:08
507 查看
例:
应用:post方法调接口
转自:http://www.jb51.net/article/20705.htm
另外 :http://bbs.csdn.net/topics/370026897 调用接口 (get方法),
<?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>
相关文章推荐
- PHP通过XML报文格式的POST请求方式,与第三方接口交互(发送xml,获取XML,并解析xml步骤)
- Android HttpURLConnection(Get,Post方式)进行网络通信 获取数据和网络图片
- PHP发送POST请求的三种方式 分别使用curl file_get_content fsocket 来实现post提交数据
- 使用HttpURLConnection开发自动发送Get、Post请求并获取响应
- restful接口客户端和服务端开发,HttpURLConnection,HttpClient,post ,get方式调用
- HttpUrlConnection和HttpClient两种方式获取GET和POST请求
- python flask 服务器端实现接口,post,get 方式,参数获取方法
- http请求远程接口获得JSON数据(1) --- 利用HttpURLConnection发送post/get请求
- 【转】java通用URL接口地址调用方式GET和POST方式
- java发送post和get请求(1)---HttpURLConnection方式
- 向指定URL发送GET或POST请求(调第三方接口)
- HTTP 请求方式: GET和POST的比较当发送数据时,GET 方法向 URL 添加数据;URL 的长度是受限制的(URL 的最大长度是 2048 个字符)。
- C#模拟Post和Get方式发送数据
- Http学习之使用HttpURLConnection发送post和get请求
- C#模拟Post和Get方式发送数据的技巧
- android中post和get两种方式发送请求
- Http学习之使用HttpURLConnection发送post和get请求
- ASP.NET后台手动调用POST提交并获取返回值 用于短信平台接口
- 在ASP.NET中获取参数POST和GET方式提交的参数
- HttpURLConnection发送post和get请求