GET
$curl = curl_init();
//设置抓取的url
$url="请求url"; //地址要拼接上请求参数
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 1); //设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//设置获取的信息以文件流的形式返回,而不是直接输出
$data = curl_exec($curl); //执行命令
curl_close($curl); //关闭URL请求
return ($data); //显示获得的数据
POST
$url = "请求的url";
$postData = array(
'请求参数'=>请求参数的值
)
$postData = http_build_query($postData); //做一层过滤
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData );
$result=curl_exec ($ch);
curl_close($ch);
GET 和 POST 简单应用 ,不用传header参数
<?php
class Action
{
public static function curl_get($url){
$testurl = $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $testurl);
//参数为1表示传输数据,为0表示直接输出显示。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//参数为0表示不带头文件,为1表示带头文件
curl_setopt($ch, CURLOPT_HEADER,0);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
/*
* url:访问路径
* array:要传递的数组
* */
public static function curl_post($url,$array){
$curl = curl_init();
//设置提交的url
curl_setopt($curl, CURLOPT_URL, $url);
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 0);
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//设置post方式提交
curl_setopt($curl, CURLOPT_POST, 1);
//设置post数据
$post_data = $array;
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
//执行命令
$data = curl_exec($curl);
//关闭URL请求
curl_close($curl);
//获得数据并返回
return $data;
}
}
发送post请求且header中带参数bug调试(重点)
通常get方式header中带参数如下通过curl调用即可:
function send_get_curl_header($url, $data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
注意$data格式为数组['AppToken: '.$token],而post发送时要注意postData要进行http_build_query才能成功代码如下:
function send_post_curl_header($url, $header_data , $post_data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_data);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
$sResult = curl_exec($ch);
if($sError=curl_error($ch)){
die($sError);
}
curl_close($ch);
return $sResult;
}
php使用curl模拟post请求 带header 参数
$url="http://localhost/header_server.php";
$body = array("mobile"=>"13899999999", "username"=>"Nick");
$header = array("Content-Type:multipart/x-www-form-urlencoded", "token:test", "client:h5");
注意里面的content-type 参数可以为空 $header = array("token:test", "client:h5");
$result = curlPost($url, $body, 5, $header, 'json');
var_dump($result);
/**
* 传入数组进行HTTP POST请求
*/
function curlPost($url, $post_data = array(), $timeout = 5, $header = "", $data_type = "") {
$header = empty($header) ? '' : $header;
//支持json数据数据提交
if($data_type == 'json'){
$post_string = json_encode($post_data);
}elseif($data_type == 'array') {
$post_string = $post_data;
}elseif(is_array($post_data)){
$post_string = http_build_query($post_data, '', '&');
}
$ch = curl_init(); // 启动一个CURL会话
curl_setopt($ch, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查 // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
//curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的Post请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); // Post提交的数据包
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // 设置超时限制防止死循环
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
//curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模拟的header头
$result = curl_exec($ch);
// 打印请求的header信息
//$a = curl_getinfo($ch);
//var_dump($a);
curl_close($ch);
return $result;
}
几点经验:
1. 不管"Content-Type:multipart/form-data"还是"Content-Type:application/x-www-form-urlencoded"只要采用post方式发送数据,并且在body体中的数据是数组格式,那么在接收端就可以使用$_POST获取到。
2. 在接收端使用file_get_contents("php://input")接收时,只能获取到字符串类型的body体数据。
3. 请求时,在header中添加的参数xxx,在接收端可以使用$_SERVER["HTTP_XXX"]进行获取。
https_request 同时支持post get
//调用 测试接口
public function testapi(){
$pwd = 'Pass1234';
$uid = 'test01';
//获取token
$token = $this->getToken(); //我自己的另外的方法
//获取加密后的密文
$credential = $this->publicEncrypt($pwd);//我自己的另外的方法
//自定义的header数据
$header=array('Content-Type:application/json','Authorization:Bearer '.$token['token'],); // 这里json 形式输出
//备注 有以下几种形式输出
// Authorization 是Headers 里面的参数
// array("Content-Type:multipart/x-www-form-urlencoded") // 以urlencoded 输出
// array() // 以空数组形式输出
//请求地址
$sUrl = 'https://测试url';
//post数据
$aData = array(
'uid' => $uid,
'credential' => $credential,
);
$sResult = $this->httpRequest($sUrl,$header,$aData);
$aResData = json_decode($sResult, true);
//显示返回数据
echo "<pre>";
print_r($aResData);
die;
}
//curl 模拟http请求 支持post get
function http_request($url,$headers = array(),$data = null)
{
// php curl 发起get或者post请求
// curl 初始化
$curl = curl_init(); // curl 设置
if (count($headers) >= 1) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);// 校验证书节点
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);// 校验证书主机
// 判断 $data get or post
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);// 以文件流的形式 把参数返回进来
// 如果这一行 不写你就收不到 返回值
// 执行
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
备注:
$opt_data = json_encode($data);
$header = array();
$header[] = 'Authorization:'.$tmp;
$header[] = 'Accept:application/json';
$header[] = 'Content-Type:application/json;charset=utf-8';
$curl = curl_init(); //初始化
curl_setopt($curl,CURLOPT_URL,$url); //设置url
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
curl_setopt($curl,CURLOPT_POSTFIELDS,$opt_data);
php curl 设置请求头headers和请求体body
$url = "http://www.example.com";
//headers数组内的格式
$headers = array();
$headers[] = "app-id:xxxxx";
$headers[] = "Content-Type:application/json";
$body = array(
"username" => "username",
"password" => "password"
);
$postBody = json_encode($body);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//设置请求头
curl_setopt($curl, CURLOPT_POSTFIELDS, $postBody);//设置请求体
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');//使用一个自定义的请求信息来代替"GET"或"HEAD"作为HTTP请求。(这个加不加没啥影响)
$data = curl_exec($curl);
echo $data;
PHP curl请求get和Post的请求方法+Headers参数
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
————————————————
原文链接:https://blog.youkuaiyun.com/yang_guang91/article/details/83621049
PHP curl get和post 请求头带参数的方法
GET 方法:
public function curlGet($url){
$header = array(
'APIKEY:'.$this->APIKEY,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
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);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
post 方法:
public function curlPost($url,$post_data = array()){
$header = array(
'APIKEY:'.$this->APIKEY,
);
if (is_array($post_data))
{
$post_data = http_build_query($post_data, null, '&');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch , CURLOPT_URL , $url);
curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch , CURLOPT_POST, 1);
curl_setopt($ch , CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
public function test($url, $body, $headers) {
$postBody = http_build_query($body);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//设置请求头
curl_setopt($curl, CURLOPT_POSTFIELDS, $postBody);//设置请求体
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');//使用一个自定义的请求信息来代替"GET"或"HEAD"作为HTTP请求。(这个加不加没啥影响)
$data = curl_exec($curl);
var_dump($data);
}
调用 :方法
//$headers = array("Content-Type:multipart/x-www-form-urlencoded", 'Authorization:'.$Authorization, "client:h5");
/*$headers = array("", 'Authorization:'.$Authorization, "client:h5");
$body = array('video_type' => $video_type);
$this->test($url, $body, $headers);*/