简体   繁体   English

PHP中带有JSON字符串的HTTP发布请求

[英]Http post request with JSON String in PHP

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. 在url末尾,根据服务器要求将JSON字符串连接起来,但是作为响应,我没有任何响应变量。 What is wrong with this as when i make this request using chrome extension it shows me the result updated. 这是什么问题,因为当我使用chrome扩展程序发出此请求时,它显示了更新的结果。 And when i use the $url= "http://dummyhost:8080/admin/ViewSubsriber?jsonString={"sub_Name":"messi","sub_Password":"password"}"; 当我使用$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 我也用过Curl

$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 但是我得到的结果却是一样

If you have created JSON string at your own keep following things in mind: 如果您自己创建了JSON字符串,请记住以下几点:
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/ 然后在线检查该字符串,无论JSON字符串有效还是类似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=""; 用于原始数据,而不是使用空的$data_string="";

Your posted variable, $data_string, is empty. 您发布的变量$ data_string为空。 You are using POST and sending empty data, but then also sending a query string. 您正在使用POST并发送空数据,但同时还发送了查询字符串。 It seems you are mixing GET and POST methods here. 似乎您在这里混合使用GET和POST方法。 You need to actually post your JSON string in the posted data. 您实际上需要在发布的数据中发布JSON字符串。

If you are posting raw JSON string using application/JSON content type, the post data will need to be read from raw input like this 如果要使用应用程序/ JSON内容类型发布原始JSON字符串,则需要像这样从原始输入中读取发布数据

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

This is because $_POST is only automatically populated by PHP for form-encoded content types. 这是因为$ _POST仅由PHP自动填充以表单编码的内容类型。

I would also recommend sticking with curl for such usage. 对于这种用法,我也建议保持卷曲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM