Home  >  Article  >  Backend Development  >  How to write php callback interface

How to write php callback interface

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-09-25 09:38:012862browse

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(&#39;php://input&#39;);
$data = $contents ? $this->_xmlToArray($contents) : array();
//写回滚日志
$filePath = APP_PATH."/data/log/";
$this->createDirectory($filePath);    //目录不存在,则创建
$fileName = $filePath."back.txt";
file_put_contents( $fileName, date(&#39;Ymd H:i:s&#39;)."\r\n".(json_encode($data))."\r\n", FILE_APPEND | LOCK_EX);
//var_export($data);die;
$cnt = 0;
$result = $data[&#39;list&#39;][&#39;allRet&#39;];
/**根据回调结果处理我们数据库的逻辑*/
//START
foreach ($result as $k => $v){ //在循环中[根据唯一标识ID]处理自己数据了的逻辑
    $userMobile = $v[&#39;mobile&#39;];
    if($v[&#39;status&#39;] == 1 ){
        
    }else{
        
    }
    $cnt ++;
}
//END
if( $cnt == $data[&#39;count&#39;] ){
    file_put_contents( $fileName, date(&#39;Ymd H:i:s&#39;)."\r\n".(json_encode($data))."\r\n", FILE_APPEND | LOCK_EX);
    //写结果日志
    exit(&#39;SUCCESS&#39;);    //响应第三方[在回调中返回结果,告诉人家自己是否回调成功,否则人家可能会回调N次]
}else{
    file_put_contents( $fileName, date(&#39;Ymd H:i:s&#39;)."\r\n".(json_encode($data))."\r\n", FILE_APPEND | LOCK_EX);
    //写结果日志
    exit(&#39;ERROR&#39;);
}
}
//XML格式化成数组
function _xmlToArray($xml){
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, &#39;SimpleXMLElement&#39;, 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 = &#39;<?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>&#39;;
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn