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, false); // 从证书中检查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);
}
if (!curl_errno($curl)) {
echo json_encode(['code'=>1001,'msg'=>'网络或者URL出错!','data'=>curl_errno($curl)]);
exit();
}
curl_close($ch);
2:GET请求
$start_time=
urlencode
(date('Y-m-d H:i:s',$five));
$end_time=
urlencode
(date('Y-m-d H:i:s',time()));
$url
=
"http://www.xxx.com/White/CardInfo?cardNo="
.
$bank_card
."&start_time=2021-07-11 10:20:00&end_time=2021-07-11 10:25:59"; //
curl请求返回空白
,urlencode()函数用于将中文进行转码操作
//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, false); // 从证书中检查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);
}
if (!curl_errno($curl)) {
echo json_encode(['code'=>1001,'msg'=>'网络或者URL出错!','data'=>curl_errno($curl)]);
exit();
}
curl_close($ch);
3:获取域名后的路径
$_SERVER['REQUEST_URI'];//获取URL
如果在php中跨域请求,出现404,则是路径不对。header("Access_Control_Allow_Origin:http:*");