本文目錄一覽:
- 1、如何用curl post 一段包含中文json的文本到伺服器
- 2、如何用php調用外部介面json數據
- 3、為什麼要使用curl傳輸json
- 4、Curl命令詳解
- 5、如何使用curl將數組放入json對象
如何用curl post 一段包含中文json的文本到伺服器
我的博客《PHP cURL實現模擬登錄與採集使用方法詳解》第十一點發送與獲取json數據對此類問題做了詳細的講解,下面是代碼示例:
?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;
#\n分割數據
$data = [
‘name:Zjmainstay’,
‘website:’,
];
$data = implode(“\n”, $data);
#分割數據
$data = ‘name:Zjmainstaywebsite:’;
更多詳情,包括服務端如何接收此類數據,請查看博客:
如何用php調用外部介面json數據
兩種比較簡單的方法:
1、使用curl
$url = “”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT , 30);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
2、使用file_get_contents
$output = file_get_contents($url);
echo $output;
3 、使用socket 也是可以的
為什麼要使用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;
}
Curl命令詳解
-#, –progress-bar
顯示進度條
-b, –cookie name=data
使用cookie。如果沒有 = , 則表示cookie文件路徑 (參考 -c )
-c, –cookie-jar file name
response的cookie保存路徑
-d, –data data
POST請求數據
-f, –fail
忽略錯誤信息 (不顯示返回的HTML錯誤信息)
-F, –form name=content
表單數據
-H, –header header
設置請求Header
-i, –include
輸出請求Header信息
-I, –head
只顯示Header信息
-k, –insecure
允許不安全鏈接
-L, –location
Follow redirects.
-o, –output file
輸出信息保存到指定文件中。可與 –create-dirs 一起使用,自動創建文件路徑
-O, –remote-name
輸出信息寫到文件中,文件名同伺服器端的文件名 (只能寫入到當前目錄)
-s, –silent
靜默模式。與 -S 一起用,強制輸出errors信息
-v, –verbose
顯示更多信息(用於調試).
-w, –write-out format
請求結果後追加內容。例如, -w “\n” 可以在輸出結果後追加一個換行符。可以把 -w “\n” 添加到 ~/.curlrc 文件中,這樣每次執行結果後都自動追加換行符(默認curl返回內容最後缺少換行符,顯示不友好)
-X, –request
請求方法類型,POST、GET、PUT等
使用 POST 或 PUT 請求時, 可用 Content-Type 指定兩種數據格式:
curl默認為表單格式。如果使用json格式,需要手動設置header。
對於 POST 和 PUT 請求, 以下是通用參數:
application/x-www-form-urlencoded 為默認值:
等效於:
也可以使用數據文件:
或使用數據文件:
如何使用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字元串轉化為數組
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/286328.html