PHP post调接口代码

php

浏览数:265

2019-1-7

AD:资源代下载服务
 /**
 * $url:接口地址
 * $data:数组参数
 **/
 function postData($url, $data) {
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); 

        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
        $file_contents = curl_exec ( $ch );
        if (curl_errno ( $ch )) {
                return 'Errno' . curl_error ( $ch );
        }
        // echo $file_contents;
        curl_close ( $ch );

        return $file_contents;
    }