1. 程式人生 > >PHP使用CURL設置header頭傳參以及設置Content-Type: application/json類型的後臺數據接收

PHP使用CURL設置header頭傳參以及設置Content-Type: application/json類型的後臺數據接收

字符串 remote gate nginx 請求方式 bec php work str

CURL函數

public function CurlRequest($url,$data=null,$header=null){
  //初始化瀏覽器
  $ch = curl_init();
  //設置瀏覽器,把參數url傳到瀏覽器的設置當中
  curl_setopt($ch, CURLOPT_URL, $url);
  //以字符串形式返回到瀏覽器當中
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  //禁止https協議驗證域名,0就是禁止驗證域名且兼容php5.6

  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  //禁止https協議驗證ssl安全認證證書
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  //判斷data是否有數據,如果有data數據傳入那麽就把curl的請求方式設置為POST請求方式
  if ( !empty($data) ) {
    //設置POST請求方式
    @curl_setopt($ch, CURLOPT_POST, true);
    //設置POST的數據包
    @curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  }
  //設置header頭
  if ( !empty($header) ) {
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  }
  //讓curl發起請求
  $str = curl_exec($ch);
  //關閉curl瀏覽器
  curl_close($ch);
  //把請求回來的數據返回
  return $str;
}

函數調用

$url = "http://desk.yunindex.cn/test";
$jsonArr = [
  ‘accountid‘ => ‘34‘,
  ‘option‘ => ‘chat_index‘,
];
$jsonStr = json_encode($jsonArr);  //設置了Content-Type: application/json,傳參要轉化為JSON,否則後臺接收不到數據

//header頭設置
$headers = array(
  "Content-Type: application/json",
  "Content-Length: " . strlen($jsonStr) . "",
  "Accept: application/json",
  "client-id:5K8264ILTKCH16CQ2502SI8ZNMTM67VS",    //自定義參數
  "client-secret:C380BEC2BFD727A4B6845133519F3AD6"  //自定義參數
);
$data = $this->CurlRequest($url,$jsonStr,$headers);

後臺接受

json_encode($_SERVER)  

使用$_SERVER能接收到header頭的信息,在header頭裏面設置的自定義參數會在這裏面體現

結果

{
"USER": "www",
"HOME": "\/home\/www",
"HTTP_CLIENT_SECRET": "C380BEC2BFD727A4B6845133519F3AD6",       //這是自定義的參數,跟上面傳的參數相對於,會自動轉化為大寫跟下劃線
"HTTP_CLIENT_ID": "5K8264ILTKCH16CQ2502SI8ZNMTM67VS",          //這是自定義的參數,跟上面傳的參數相對於,會自動轉化為大寫跟下劃線
"HTTP_ACCEPT": "application\/json",  
"HTTP_CONTENT_LENGTH": "40",
"HTTP_CONTENT_TYPE": "application\/json",
"HTTP_HOST": "desk.yunindex.cn",
"SCRIPT_FILENAME": "\/workspace\/php\/index_desk\/htdocs\/index.php",
"REDIRECT_STATUS": "200",
"SERVER_NAME": "desk.yunindex.cn",
"SERVER_PORT": "80",
"SERVER_ADDR": "192.168.1.90",
"REMOTE_PORT": "64524",
"REMOTE_ADDR": "192.168.1.113",
"SERVER_SOFTWARE": "nginx\/1.15.2",
"GATEWAY_INTERFACE": "CGI\/1.1",
"REQUEST_SCHEME": "http",
"SERVER_PROTOCOL": "HTTP\/1.1",
"DOCUMENT_ROOT": "\/workspace\/php\/index_desk\/htdocs",
"DOCUMENT_URI": "\/index.php",
"REQUEST_URI": "\/test",
"SCRIPT_NAME": "\/index.php",
"CONTENT_LENGTH": "40",
"CONTENT_TYPE": "application\/json",
"REQUEST_METHOD": "POST",
"QUERY_STRING": "",
"FCGI_ROLE": "RESPONDER",
"PHP_SELF": "\/index.php",
"REQUEST_TIME_FLOAT": 1557025085.410574,
"REQUEST_TIME": 1557025085
}

這裏有一個問題,如果使用的是nginx的話,header頭裏的參數如果有下劃線(如:client_secret),使用$_SERVER貌似會接收不到該參數,需要配置nginx.conf,在裏面添加 underscores_in_headers on; 配置

file_get_contents(‘php://input‘)

接收參數,以流的形式接收

結果

string(40) "{"accountid":"34","option":"chat_index"}"

PHP使用CURL設置header頭傳參以及設置Content-Type: application/json類型的後臺數據接收