php网络请求

get请求

/**
 * get请求
 * @param $url,请求地址
 * @return bool|string
 */
function getRequest($url){
    $headerArray =array("Content-type:application/json;","Accept:application/json");
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
    $output = curl_exec($ch);
    curl_close($ch);
    //将返回值打包为json对象并返回
    //$output = json_decode($output,true);
    return $output;
}

post请求

/**
 * post请求
 * @param $url,请求地址
 * @param $data,网络请求参数,进行过decode的编码的json数组
 * @return bool|string
 */
function postRequest($url,$data){
    $data  = json_encode($data);
    $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    //将返回值打包为json对象并返回
    //$output = json_decode($output,true);
    return $output;
}

 

上一篇:PHP使用聚合图床的上传接口PHP版和html版


下一篇:PHP CURL调用接口