Home > Article > Backend Development > How to write php callback interface
1. Third-party callback API description:
Interface address: None [No need to configure in the third-party background]
Request method: None
Request parameters: None
Return parameters
type: SMS type: 1 Advertisement; 2 Blessings
count: The total number of SMS messages pushed this time
id: Unique identifier
mobile: SMS number
userId : User account
status: Sending status: 0 failed; 1 successful
Return result: [XML format]
Related recommendations: "php basics Tutorial》
For example:
<?xml version="1.0" encoding="utf-8"?> <returnData> <type>1</type> <count>2</count> <list> <allRet> <id>1529051684657847</id> <mobile>187********</mobile> <status>0</status> <userId>zzzppp</userId> </allRet> <allRet> <id>1529051612347847</id> <mobile>137********</mobile> <status>1</status> <userId>wwwhhh</userId> </allRet> </list> </returnData>
Response description: The client receives successfully, please return: 100 or OK or SUCCESS
2. PHP callback code:
//PHP接收回调地址操作 public function backAction(){ //模拟获取接收的数据 $contents = $this->getData(); //本地模拟接口获取到的数据 //$contents = file_get_contents('php://input'); $data = $contents ? $this->_xmlToArray($contents) : array(); //写回滚日志 $filePath = APP_PATH."/data/log/"; $this->createDirectory($filePath); //目录不存在,则创建 $fileName = $filePath."back.txt"; file_put_contents( $fileName, date('Ymd H:i:s')."\r\n".(json_encode($data))."\r\n", FILE_APPEND | LOCK_EX); //var_export($data);die; $cnt = 0; $result = $data['list']['allRet']; /**根据回调结果处理我们数据库的逻辑*/ //START foreach ($result as $k => $v){ //在循环中[根据唯一标识ID]处理自己数据了的逻辑 $userMobile = $v['mobile']; if($v['status'] == 1 ){ }else{ } $cnt ++; } //END if( $cnt == $data['count'] ){ file_put_contents( $fileName, date('Ymd H:i:s')."\r\n".(json_encode($data))."\r\n", FILE_APPEND | LOCK_EX); //写结果日志 exit('SUCCESS'); //响应第三方[在回调中返回结果,告诉人家自己是否回调成功,否则人家可能会回调N次] }else{ file_put_contents( $fileName, date('Ymd H:i:s')."\r\n".(json_encode($data))."\r\n", FILE_APPEND | LOCK_EX); //写结果日志 exit('ERROR'); } } //XML格式化成数组 function _xmlToArray($xml){ libxml_disable_entity_loader(true); $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $val = json_decode(json_encode($xmlstring),true); return $val; } //递归创建目录[在linux下要有创建目录的权限才能创建目录 chmod -R 777 /var/data/log ] public function createDirectory( $dir ){ return is_dir ( $dir ) or $this->createDirectory(dirname( $dir )) and mkdir ( $dir , 0777); } public function getData(){ $xml = '<?xml version="1.0" encoding="utf-8"?> <returnData> <type>1</type> <count>2</count> <list> <allRet> <id>1529051684657847</id> <mobile>187********</mobile> <status>0</status> <userId>zzzppp</userId> </allRet> <allRet> <id>1529051612347847</id> <mobile>137********</mobile> <status>1</status> <userId>wwwhhh</userId> </allRet> </list> </returnData>'; return $xml; }
The above is the detailed content of How to write php callback interface. For more information, please follow other related articles on the PHP Chinese website!