本文目錄一覽:
- 1、如何用curl post 一段包含中文json的文本到伺服器
- 2、php通過curl發送post json給https產生502錯誤問題!
- 3、為什麼要使用curl傳輸json
- 4、PHP如何通過Post請求發送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實現模擬登錄與採集使用方法詳解教程》
php通過curl發送post json給https產生502錯誤問題!
curl_setopt(self::$ch, CURLOPT_USERAGENT, “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/600.3.18 (KHTML, like Gecko) Version/8.0.3 Safari/600.3.18”);
加一個這個參數模擬瀏覽器,看看有沒有作用
有些伺服器非瀏覽器訪問就會502
為什麼要使用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;
}
PHP如何通過Post請求發送Json數據
首先要把數據轉換成json格式,再通過curl方法調用介面並傳參數
代碼如下:
$keyword = urlencode($_POST[‘keyword’]);
$parameters = json_encode(array(‘keyWord’=$keyword,’areaCode’=’*’));
$post_data[‘appToken’] = “323ds7674354fds32fdsda60173”;//隨便寫的
$post_data[‘parameters’] = $parameters;
$url = ”;//隨便寫的
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//用post方法傳送參數
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
之後就返回數據即可。
原創文章,作者:TNL5J,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/128395.html