douchuituo3032 2014-04-03 05:23
浏览 59
已采纳

使用PHP中的JSON字符串发送Http post请求

Here is my code,

$url= 'http://dummyhost:8080/admin/EditSubscriber?jsonString={"sub_Id":3,"sub_Fname":"messi","sub_Lname":"lionel"}';
$data_string="";
$request = new HTTPRequest($url, HTTP_METH_POST);
$request->setRawPostData($data_string);
$request->send();    
$response = $request->getResponseBody();
$response= json_decode($response, true);

at the end of url JSON string is concatenated according to server requirement but in response there is nothing i get in response variable. What is wrong with this as when i make this request using chrome extension it shows me the result updated. And when i use the $url= "http://dummyhost:8080/admin/ViewSubsriber?jsonString={"sub_Name":"messi","sub_Password":"password"}"; i get the desired result. i've used curl as well'

i've used Curl as well like this

$ch = curl_init($url);                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
'Content-Type: application/json',                                                                                
'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   
$result = curl_exec($ch);                            
curl_close($ch);
$json_result = json_decode($result, true);

but the same result i get that is nothing

  • 写回答

2条回答 默认 最新

  • duanfei8149 2014-04-09 11:54
    关注

    If you have created JSON string at your own keep following things in mind:
    Space in string might result in unnatural behavior from server so for each of the varible or atleast for strings use

    urlecode(yourvariable);
    

    then chek the string online wether the JSON string is valid or not like this http://json.parser.online.fr/

    Like Brant says use

    $json = file_get_contents('php:://input');
    

    for raw data instead of using the empty $data_string="";

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭