To avoid the duplication of API and project background call data, you can directly call the API information test

To avoid the duplication of api and project background call data, you can directly call the api information test, and simulate the browser behavior through the curl module, which is convenient for background development:

function get_api_data($api_params,$send_data,$mode = ''){

    $api_config = /...../;

    $method = /..../;

    $ api_url = /...../;

    if($method && $api_url){

        $ch = curl_init();

        $query_str = '';

        if($method == 'get'){

            if($mode == 'json'){

                $query_str = '?'.$send_data;

            }else{

                $query_str = '?'.http_build_query($send_data);

            }     

        }

        curl_setopt ($ch, CURLOPT_URL, $api_url.$query_str);

        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

        if($method == 'post'){

            if($mode == 'json'){

                curl_setopt ($ch, CURLOPT_POST, 1);

                curl_setopt($ch, CURLOPT_POSTFIELDS,$send_data);

                curl_setopt($ch, CURLOPT_HTTPHEADER, array(

                    'Content-Type: application/json; charset=utf-8',

                    'Content-Length: '.strlen($send_data))

                );

            }else{

                curl_setopt ($ch, CURLOPT_POST, 1);

                curl_setopt ($ch, CURLOPT_POSTFIELDS,http_build_query($send_data));

            }

        }

        $response = curl_exec($ch);

        curl_close($ch);

        $responseJson = json_decode($response);

        return $responseJson;   

    }else{

        return -1; //Configuration call error

    }    

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326267858&siteId=291194637