本文目錄一覽:
- 1、如何用curl post 一段包含中文json的文本到服務器
- 2、如何使用curl將數組放入json對象
- 3、windows下使用curl利用post發送json數據時注意事項
- 4、php用curl的post方法傳遞json包的時候,接受方是怎麼獲取的呢
- 5、為什麼要使用curl傳輸json
如何用curl post 一段包含中文json的文本到服務器
一般中文json_encode之後會變成\uxxxx的格式了,只要使用正規的json_encode處理,
不需要考慮中文問題。
至於如何post數據到服務器,需要設定header,參考代碼如下:
?php
#json數據
$url = ”;
$data = ‘{“a”:”b”}’;
$length = strlen($data);
$header = array(
‘Content-Length: ‘ . $length, //不是必需的
‘Content-Type: text/json’,
);
$ch = curl_init($url); //初始化curl
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$content = curl_exec($ch); //執行並存儲結果
curl_close($ch);
echo $content;
服務端需要使用$data = file_get_contents(‘php://input’);獲取數據。
更多PHP cURL內容請參考我的博客《PHP cURL實現模擬登錄與採集使用方法詳解教程》
如何使用curl將數組放入json對象
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, ORDERPOSTURL); //抓取指定網頁
curl_setopt($ch, CURLOPT_HEADER, 0); //設置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //設置是否返回信息
curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);//發送數據
$response = curl_exec($ch); //接收返回信息
if (curl_errno($ch)) {
//出錯則記錄錯誤信息
Logger::getLogger(“reqLogger”)-error(“錯誤信息:” . curl_error($ch));
}
curl_close($ch); //關閉curl鏈接
$obj=json_decode($myLogger);//json字符串轉化為對象
$arry=json_decode($response,true);//json字符串轉化為數組
windows下使用curl利用post發送json數據時注意事項
在window中linux格式下的單引號要改成雙引號,json格式數據中雙引號要加\轉義
php用curl的post方法傳遞json包的時候,接受方是怎麼獲取的呢
假設POST的數據為:{“data”:”abc”}
POST參數為:data
同樣以PHP為例,接受並處理請求的相關代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
?php
extract($_POST); // 將數組中的key攤成變量,並導入key對應的值
if (!empty($data))
{
$data = json_decode($data); // json 字符串解碼成 json 數據
var_dump($data); // 打印 json 數據
// 輸出結果
object(stdClass)[1]
public ‘data’ = string ‘abc’ (length=3)
}
為什麼要使用curl傳輸json
//使用curl庫,以post方式向服務器發送json數據
//json數據的組合可以參考jsoncpp庫,也可以按json格式自己組合字符串
//注意事項,以下代碼不可以多線程執行,如果多線程執行,需要加鎖進行控制,否則會運行崩潰
[cpp] view plain copy
#include curl/curl.h
#include string
#include exception
int main(int argc, char *argv[])
{
char szJsonData[1024];
memset(szJsonData, 0, sizeof(szJsonData));
std::string strJson = “{“;
strJson += “\”user_name\” : \”test\”,”;
strJson += “\”password\” : \”test123\””;
strJson += “}”;
strcpy(szJsonData, strJson.c_str());
try
{
CURL *pCurl = NULL;
CURLcode res;
// In windows, this will init the winsock stuff
curl_global_init(CURL_GLOBAL_ALL);
// get a curl handle
pCurl = curl_easy_init();
if (NULL != pCurl)
{
// 設置超時時間為1秒
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 1);
// First set the URL that is about to receive our POST.
// This URL can just as well be a
// https:// URL if that is what should receive the data.
curl_easy_setopt(pCurl, CURLOPT_URL, “”);
//curl_easy_setopt(pCurl, CURLOPT_URL, “”);
// 設置http發送的內容類型為JSON
curl_slist *plist = curl_slist_append(NULL,
“Content-Type:application/json;charset=UTF-8”);
curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, plist);
// 設置要POST的JSON數據
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, szJsonData);
// Perform the request, res will get the return code
res = curl_easy_perform(pCurl);
// Check for errors
if (res != CURLE_OK)
{
printf(“curl_easy_perform() failed:%s\n”, curl_easy_strerror(res));
}
// always cleanup
curl_easy_cleanup(pCurl);
}
curl_global_cleanup();
}
catch (std::exception ex)
{
printf(“curl exception %s.\n”, ex.what());
}
return 0;
}
原創文章,作者:XHMIA,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/318179.html