dream5694
2017-01-24 03:02
浏览 440
已采纳

如何在Laravel中获取和发送Json字符串?

I have simulator as you can see in the pictures. In this simulator I have a send button that will trigger sending a JSON string appended to a URL.

I test this with the URL localhost/ussd/index.php

The index.php page:

<?php

header('Content-type: application/json');
$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON, TRUE );

$data["message"]='We  Display This in Phone Screen';
$data["applicationId"]=$input["applicationId"];
$data["password"]="password";
$data["version"]="1.0";
$data["sessionId"]=$input["sessionId"];
$data["ussdOperation"]="mt-cont";
$data["destinationAddress"]=$input["sourceAddress"];
$data["encoding"]="440";
$data["chargingAmount"]="5";

$json_string = json_encode($data);
$json_url = "http://localhost:7000/ussd/send";

$ch = curl_init( $json_url );

$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
CURLOPT_POSTFIELDS => $json_string
);
curl_setopt_array( $ch, $options );

$result =  curl_exec($ch); 

?> 

Then index.php file gets this string and sends another string to that page. The simulator page gets this JSON object and displays it. This can be done in the index.php file.

But I want to do it in Laravel. I tried But not Work How can I do it.

It works like this: It works like that => image

Iindex.php file: Iindex.php file

图片转代码服务由CSDN问答提供 功能建议

我有模拟器,你可以在图片中看到。 在这个模拟器中,我有一个发送按钮,它将触发发送附加到URL的JSON字符串。</ p>

我使用URL localhost / ussd / index.php </ em进行测试 > </ p>

index.php页面:</ h3>
 &lt;?php 
 
header('Content-type:application / json'  ); 
 $ inputJSON = file_get_contents('php:// input'); 
 $ input = json_decode($ inputJSON,TRUE); 
 
 $ data [“message”] ='我们在手机屏幕中显示 “; 
 $的数据[ “的applicationID”] = $输入[ “的applicationID”]; 
 $的数据[ “密码”] = “密码”; 
 $的数据[ “版本”] = “1.0”; 
  $数据[ “的sessionId”] = $输入[ “的sessionId”]; 
 $的数据[ “ussdOperation”] = “MT-CONT”; 
 $的数据[ “目的地地址”] = $输入[ “sourceAddress”];  
 $ data [“encoding”] =“440”; 
 $ data [“chargingAmount”] =“5”; 
 
 $ json_string = json_encode($ data); 
 $ json_url =“http:/  / localhost:7000 / ussd / send“; 
 
 $ ch = curl_init($ json_url); 
 
 $ options = array(
CURLOPT_RETURNTRANSFER =&gt; true,
CURLOPT_HTTPHEADER =&gt; array('Content-type  :application / json'),
CURLOPT_POSTFIELDS =&gt; $ json_string 
); 
curl  _setopt_array($ ch,$ options); 
 
 $ result = curl_exec($ ch);  
 
&GT?;  
 </ code> </ pre> 
 
 

然后index.php文件获取此字符串并将另一个字符串发送到该页面。 模拟器页面获取此JSON对象并显示它。 这可以在 index.php </ em>文件中完成。</ p>

但我想在Laravel中完成。 我试过但不行我怎么能这样做。</ p>

它的工作原理如下: “它就像那样=&gt;图像” </ p>

Iindex.php文件: </ p> </ div>

2条回答 默认 最新

相关推荐 更多相似问题