PHP中CURL的GET和POST请求

/ 0评 / 0

http get请求:

nction httpGet($url){

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$res=json_decode(curl_exec($curl),true);

curl_close($curl);

return $res;

}

http post请求:

function httpPost($url,$post_data){

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// post数据

curl_setopt($ch, CURLOPT_POST, 1);

// post的变量

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

$output=json_decode(curl_exec($ch),true);

curl_close($ch);

return $data;

}

https get请求:

function httpsGet($url){

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);// https请求不验证证书和hosts

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

$res=json_decode(curl_exec($curl),true);

curl_close($curl);

return $res;

}

https post请求:

function httpsPost($url,$post_data){

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// post数据

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

// post的变量

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

$output=json_decode(curl_exec($ch),true);

curl_close($ch);

return $output;

}

发表评论

电子邮件地址不会被公开。 必填项已用*标注