1. 程式人生 > >php使用curl請求設定CURLOPT_HTTPHEADER為x-www-form-urlencoded時請求資料

php使用curl請求設定CURLOPT_HTTPHEADER為x-www-form-urlencoded時請求資料

之前做一個專案,和第三方平臺對接,對方要求使用Content-type為x-www-form-urlencoded傳送資料,

我依照國際慣例 把自己之前寫的 curl請求搬過來,

附上自己的curl方法

/**
 * curl_get_contents
 * @param  mixed  $url           請求url
 * @param  mixed  $ispost        是否是post請求
 * @param  mixed  $post_data     post請求資料
 * @return mixed  $output        返回資料
 * @author winter
 * 2017年6月15日09:48:52
 */
function curl_get_contents($url = '', $ispost = 0, $post_data = array())
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);


    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 對認證證書來源的檢查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 從證書中檢查SSL加密演算法是否存在
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模擬使用者使用的瀏覽器
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自動跳轉
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // 自動設定Referer
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 設定超時限制防止死迴圈
    curl_setopt($ch, CURLOPT_HEADER, 0); // 查詢顯示返回的Header區域內容
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 獲取的資訊以檔案流的形式返回
    if ($ispost)
    {        
        curl_setopt($ch, CURLOPT_POST, $ispost);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    }
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
然後按網上收到的方法 加上這句話
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
發現不行。。。。然後又各種搜各種各樣的header。。。都屎了。
無奈還是去官網看看吧。
雖然 一堆英文不好看,但是卻說的很明白

意思搭眼一看 就明白了 原來  from-data用array格式傳送,
content-type為x-www-form-urlencoded時用key=value&key=value的形式傳送
看到這 立馬回去改data
完成oK!

相關推薦

php使用curl請求設定CURLOPT_HTTPHEADERx-www-form-urlencoded請求資料

之前做一個專案,和第三方平臺對接,對方要求使用Content-type為x-www-form-urlencoded傳送資料, 我依照國際慣例 把自己之前寫的 curl請求搬過來, 附上自己的curl方法 /** * curl_get_contents * @para

http|x-www-form-urlencoded新增陣列資料資料問題

由來 上次在開發Android時,遇到一個問題,關於資料介面HTTP請求的引數的問題,使用POST方法,需要傳輸陣列資料,折騰了好久才搞定,特來記錄下。 分析 x-www-form-urlencoded字元編碼的POST請求引數會被解析成文字

axios將數據請求格式修改application/x-www-form-urlencoded

enc TE amp transform class -type efault div RM axios.defaults.headers[‘Content-Type‘] = ‘application/x-www-form-urlencoded;charset=UTF-8

請求頭header裏的contentTypeapplication/json和capplition/x-www-form-urlencoded

soft 請求 告訴 tty 消息主體 ati head orm str application/x-www-form-urlencoded 最常見的 POST 提交數據的方式了。瀏覽器的原生 <form> 表單,如果不設置 enctype 屬性,那麽最終就會以

微信小程序使用content-type等於x-www-form-urlencoded方式使用request請求數據

user ESS sharp lis 小程序開發 功能 request請求 你是 for 因為服務器只能接收x-www-form-urlencoded方式接收前端收到的數據 所以微信小程序開發的時候,必須鼓搗這個問題。 微信默認使用content-type是 applica

POST 請求中 multipart/form-data、 application/x-www-form-urlencoded 的區別

clas test 我們 style 兩個 分享圖片 content pla -type 以 Postman 為工具來看一下這兩個編碼類型的數據格式,假設我們需要傳輸的數據為 { "name": "test", "age": 18 }

springmvc+jsp中關於JQuery ajax提交的Content-Type引數設定application/json和application/x-www-form-urlencoded區別

介紹 本人頁面是用的JSP,後臺用的Spring MVC。 使用JQ的ajax需要設定Content-Type,Content-Type的設定有以下幾種常用的 "Content-Type": "application/x-www-form-urlencoded" // 適用於

http-proxy-middleware nodejs post請求超時問題 x-www-form-urlencoded

var proxy = require('http-proxy-middleware'); var options = { target: 'http://localhost:80/qssh', // 目標主機 changeOrigin: true,

解決HttpClient工具中application/x-www-form-urlencoded表單提交請求引數中文亂碼問題

一、引數亂碼現象 當我去請求第三方介面時,介面接收格式為Form表單的時候,使用HttpClient工具類。這時,對於封裝進HttpPost物件裡的請求引數,如果有中文引數,會出現亂碼的現象。 二、程式碼現象復現 controller層 @RestContr

python實現http post四種請求x-www-form-urlencoded ,form-data ,json,xml

HTTP 協議規定 POST 提交的資料必須放在訊息主體(entity-body)中,但協議並沒有規定資料必須使用什麼編碼方式。常見的四種編碼方式如下:  1、application/x-www-form-urlencoded  這應該是最常見的 POST 提交資料的方式

【記錄一次坑經歷】axios使用x-www-form-urlencoded 伺服器報400(錯誤的請求。 )(後端.Net MVC5 WebApi OAuth,前端Electron-Vue)

首先放上原始碼 electron-vue axios 註冊 axios.defaults.baseURL = 'http://localhost:8888/' axios.defaults.headers.post['Content-Type'] = 'applicatio

python urlparse庫將application/x-www-form-urlencoded轉換字典

data of type application/x-www-form-urlencoded轉換為字典形式 python官網有解釋:https://docs.python.org/2/library/urlparse.html urlparse.parse_qs(qs[

x-www-form-urlencoded與multipart/form-data區別

app 所有 get filename tar 應該 www mon view http://blog.chinaunix.net/uid-7210505-id-329700.html application/x-www-form-urlencoded 與 multipar

Spring MVC 前後臺傳遞json格式數據 Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

support style logs ica spring enc json格式數據 分享 技術 報錯如下: Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported

spring學習十二 application/x-www-form-urlencoded還是application/json

dto top attribute content oid line ipa post postman application/x-www-form-urlencoded還是application/json get、 POST 用哪種格式? 後臺如何得到這些值?

application/x-www-form-urlencoded

方式 enc 數據轉換 名稱 窗體 編碼方式 轉換成 append 封裝 application/x-www-form-urlencoded:是一種編碼格式,窗體數據被編碼為名稱/值對,是標準的編碼格式。當action為get時候,瀏覽器用x-www-form-urlenc

angular2 post以“application/x-www-form-urlencoded”形式傳參的解決辦法

form jwt orm 傳參 sub error csdn user pass angular2 post以“application/x-www-form-urlencoded”形式傳參的解決辦法 http://blog.csdn.net/tianjun2012/arti

application/json 與 application/x-www-form-urlencoded的簡單比較

tpm bject method framework code char bind parameter lee application/x-www-form-urlencoded 提交請求示例 curl -X POST ‘http://localhost:8080/form

httpclient x-www-form-urlencoded

exception cas sage mobile pool bbb return pub exce 1. 使用Apache httpclient提交post請求 http工具方法(需指定編碼, 否則出錯,這裏用的UTF-8) public static String p

7.POSTMAN中 form-data、x-www-form-urlencoded、raw、binary的區別

gravity details code 並且 src ont 上傳 bin 字段 原文地址:http://blog.csdn.net/ye1992/article/details/49998511 1、form-data: 就是