php提交raw_PHP中如何POST提交raw数据?
上圖是使用POSTMAN工具調(diào)試的,用raw形式提交數(shù)據(jù)就會返回最底部的那串?dāng)?shù)據(jù);
而下圖,則是使用form-urlencoded形式提交的數(shù)據(jù),返回錯誤了。使用第1種form-data形式也是如此。
現(xiàn)在的情況是,我使用PHP的curl來post數(shù)據(jù),則如上圖所示一樣的錯誤。
代碼如下:
function curls($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-AjaxPro-Method:ShowList',
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$get_url = "http://abc.xxx.com/UC_News_ShowList,App_Web_abc.ashx";
$post_str = '{"P":5,"Sclass":"新聞","Table":"HS_N_News","Link":"News"}';
$post_datas = curls($get_url, $post_str);
echo $post_datas;
?>
邪惡的分割線: 已解決
已解決:
這個和post方式無關(guān)。而是因為少了個header
$headers = array(
"User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36",
"X-AjaxPro-Method:ShowList"
);
代碼最終形態(tài)是:
function curls($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-AjaxPro-Method:ShowList',
'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36'
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$get_url = "http://abc.xxx.com/UC_News_ShowList,App_Web_abc.ashx";
$post_str = '{"P":5,"Sclass":"新聞","Table":"HS_N_News","Link":"News"}';
$post_datas = curls($get_url, $post_str);
echo $post_datas;
POST的數(shù)據(jù)依然是json格式的數(shù)據(jù),而不是array形式的。被POSTMAN給坑了~!!!
回復(fù)內(nèi)容:
上圖是使用POSTMAN工具調(diào)試的,用raw形式提交數(shù)據(jù)就會返回最底部的那串?dāng)?shù)據(jù);
而下圖,則是使用form-urlencoded形式提交的數(shù)據(jù),返回錯誤了。使用第1種form-data形式也是如此。
現(xiàn)在的情況是,我使用PHP的curl來post數(shù)據(jù),則如上圖所示一樣的錯誤。
代碼如下:
function curls($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-AjaxPro-Method:ShowList',
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$get_url = "http://abc.xxx.com/UC_News_ShowList,App_Web_abc.ashx";
$post_str = '{"P":5,"Sclass":"新聞","Table":"HS_N_News","Link":"News"}';
$post_datas = curls($get_url, $post_str);
echo $post_datas;
?>
邪惡的分割線: 已解決
已解決:
這個和post方式無關(guān)。而是因為少了個header
$headers = array(
"User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36",
"X-AjaxPro-Method:ShowList"
);
代碼最終形態(tài)是:
function curls($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-AjaxPro-Method:ShowList',
'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36'
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$get_url = "http://abc.xxx.com/UC_News_ShowList,App_Web_abc.ashx";
$post_str = '{"P":5,"Sclass":"新聞","Table":"HS_N_News","Link":"News"}';
$post_datas = curls($get_url, $post_str);
echo $post_datas;
POST的數(shù)據(jù)依然是json格式的數(shù)據(jù),而不是array形式的。被POSTMAN給坑了~!!!
http://stackoverflow.com/questions/13099177/how-to-send-raw-post-data-with-curl-php
據(jù)說設(shè)置一個不可識別的Content-Type,POSTFILEDS還是像以前一樣傳遞一個Array就可以了
raw 就是http請求實體內(nèi)容body中最原始的數(shù)據(jù),可以自定義發(fā)送給服務(wù)器的body數(shù)據(jù),并通過設(shè)置header來確定body的類型來供服務(wù)器解析。如果不設(shè)置content-type的話,默認(rèn)為x-www-form-urlencoded。body中為參數(shù)=&參數(shù)=的形式
curl_setopt($handle, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
已解決,謝謝。
本文原創(chuàng)發(fā)布php中文網(wǎng),轉(zhuǎn)載請注明出處,感謝您的尊重!
總結(jié)
以上是生活随笔為你收集整理的php提交raw_PHP中如何POST提交raw数据?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: postgres 显示变量_sql -
- 下一篇: unity update 协程_Unit