PHP curl请求get和Post的请求方法+Headers参数
2021-01-05 17:33:15
1:POST请求方式
$url = 'https://www.baidu.com/';//请求地址
//header 参数填写方式 'tepy:2'
$header = [
'tepy:2'
];
//请求是实体数据
$content = [
'id' => 1,
'amount' => 55,
];
$ch = curl_init();
if (substr($url, 0, 5) == 'https') {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//header请求。如不需要可以去掉
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content));
$response = curl_exec($ch); //返回数据
if ($error = curl_error($ch)) {
die($error);
}
curl_close($ch);
2:GET请求
$url = 'https://www.baidu.com/';//请求地址
//header 参数填写方式 key:value 'tepy:2'
$header = [
'tepy:2'
];
$ch = curl_init();
if(substr($url,0,5)=='https'){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//header请求。如不需要可以去掉
$response = curl_exec($ch);
runlog('rtAlipay',$response);
if($error=curl_error($ch)){
die($error);
}
curl_close($ch);
3:获取域名后的路径
$_SERVER['REQUEST_URI'];//获取URL
————————————————
版权声明:本文为CSDN博主「--旧时光」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yang_guang91/article/details/83621049
发表评论: