本文目錄一覽:
- 1、在php 的curl函數來GET一個地址,得到的響應是一個json文件,怎麼來操作這個文件
- 2、如何用curl post 一段包含中文json的文本到伺服器
- 3、php用curl的post方法傳遞json包的時候,接受方是怎麼獲取的呢
- 4、為什麼要使用curl傳輸json
- 5、如何使用cURL來獲取jSON數據和解碼數據
- 6、windows下使用curl利用post發送json數據時注意事項
在php 的curl函數來GET一個地址,得到的響應是一個json文件,怎麼來操作這個文件
打開看了一下, 後綴是json, 但裡面的代碼是JavaScript代碼!
?php
$Json = file_get_contents(‘;class=logintpl=mntangram=true’);
preg_match_all(‘/bdPass\.api\.params\.login_token\=\'([^\’]*)\’\;/is’, $Json, $Ken);
$ToKen = $Ken[1][0];
echo $ToKen;
如何用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用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;
}
如何使用cURL來獲取jSON數據和解碼數據
你將$hello直接解析成了jsons 如果伺服器端要接收。 你可以在
$hello=json_en……;下一行加上
$data=array(‘hello’=$hello);
將curl_setopt($ch,CURLOPT_POSTFIELDS,$hello);改成curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
在URL端你可以用var_dump($_POST);
windows下使用curl利用post發送json數據時注意事項
在window中linux格式下的單引號要改成雙引號,json格式數據中雙引號要加\轉義
原創文章,作者:TBVW,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/135165.html