curl传递header并接收

测试发送端



<?php
//curl提交方法
 function data_ww($url, $header, $content){
        $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);
        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);
        return $response;
}

$url = 'http://www.test.com/3.php';//此处为传值需要访问的地址
$header = array('token:JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU','language:zh','region:GZ');
$data=[
    "name"=>"abc",
    "age"=>"999"
];
$content = $data;
$response = data_ww($url, $header, $content);
print_r($response);//返回的数据
 
 
 


测试接收端



<?php

$token=$_SERVER['HTTP_TOKEN'];
$langeage = $_SERVER['HTTP_LANGUAGE'];
$region =  $_SERVER['HTTP_REGION'];

$data = [
    "token"=>$token,
    "language"=>$langeage,
    "region"=>$region,
    "post"=>$_POST
];


//echo $str;
var_dump(json_encode($data));






展示结果:

ceshi.png


原文链接: curl传递header并接收 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-579.html )

发表评论

0则评论给“curl传递header并接收”