thinkphp ,php post发送json请求,接收post请求

本文介绍了一种使用PHP实现的服务间通信方案。通过发送方和服务接收方的具体代码示例,展示了如何利用HTTP请求进行跨服务的数据传递。发送方将包含手机号码的信息发送到指定URL,接收方则对接收到的数据进行验证并返回相应的客户端ID。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:https://blog.youkuaiyun.com/happy_jijiawei/article/details/62229830

---------------------------------------------

发送方的代码如下

[html]  view plain  copy
  1. <?php  
  2. namespace Api\Controller;  
  3. use Think\Controller;  
  4. class IndexController extends Controller  
  5. {  
  6.   
  7.     public function send()  
  8.     {  
  9.         ini_set("error_reporting","E_ALL & ~E_NOTICE");  
  10.           
  11.         $obj->phonenum = '1111111';  
  12.         $data =  json_encode($obj);  
  13.           
  14.         $url = "http://www.****.cn/Api/Sync/phonenum";  
  15.           
  16.         $res = $this->http_request($url, $data);  
  17.           
  18.         echo $res;  
  19.     }  
  20.       
  21.     // HTTP请求(支持HTTP/HTTPS,支持GET/POST)  
  22.     function http_request($url, $data = null)  
  23.     {  
  24.         $curl = curl_init();  
  25.         curl_setopt($curl, CURLOPT_URL, $url);  
  26.         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);  
  27.         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);  
  28.         if (! empty($data)) {  
  29.             curl_setopt($curl, CURLOPT_POST, 1);  
  30.             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);  
  31.         }  
  32.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);  
  33.         $output = curl_exec($curl);  
  34.         curl_close($curl);  
  35.         return $output;  
  36.     }  
  37. }  

接受方的代码如下

[html]  view plain  copy
  1. <?php  
  2. namespace Api\Controller;  
  3. use Think\Controller;  
  4.   
  5. class SyncController extends Controller  
  6. {  
  7.       
  8.     //判断用户手机号码是否存在  
  9.     public function phonenum()  
  10.     {  
  11.           
  12.         $postStr = isset($GLOBALS["HTTP_RAW_POST_DATA"]) ? $GLOBALS["HTTP_RAW_POST_DATA"] : "";  
  13.         /* $this->logger('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . (empty($_SERVER['QUERY_STRING']) ? "" : ("?" . $_SERVER['QUERY_STRING'])));  
  14.         $this->logger($postStr); */  
  15.           
  16.         $sync_log = M('sync_log');  
  17.         $sync_log->url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . (empty($_SERVER['QUERY_STRING']) ? "" : ("?" . $_SERVER['QUERY_STRING']));  
  18.         $sync_log->poststr = $postStr;  
  19.         $sync_log->createtime =time();  
  20.         $sync_log->add();  
  21.           
  22.         $res = json_decode($postStr, true);  
  23.           
  24.         // 验证手机号码是否存在  
  25.         $user_db = M("member_list");  
  26.         $map['member_list_tel'] = $res['phonenum'];  
  27.         $map['display'] = 'on';  
  28.         $user_res = $user_db->where($map)->find();  
  29.           
  30.           
  31.         if ($user_res) {  
  32.             $arr = array(  
  33.                 'clientid' => $user_res['member_list_id']  
  34.             );  
  35.         }else {  
  36.             $arr = array(  
  37.                 'clientid' => 0  
  38.             );  
  39.               
  40.         }  
  41.           
  42.         echo json_encode($arr);  
  43.           
  44.     }  
  45. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值