2017-01-24 03:02
浏览 262


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:


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

$data["message"]='We  Display This in Phone Screen';

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

$ch = curl_init( $json_url );

$options = array(
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字符串。

我使用URL localhost / ussd / index.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_HTTPHEADER =&gt; array('Content-type  :application / json'),
CURLOPT_POSTFIELDS =&gt; $ json_string 
curl  _setopt_array($ ch,$ options); 
 $ result = curl_exec($ ch);  

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

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

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


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douxu2467 2017-01-24 17:18

    Thank you everyone I found Answer

    web.php like that

    Route::post('test3', 'UssdController@test3');

    UssdController.php like that

    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use GuzzleHttp\Client;
    use Illuminate\Support\Facades\Log;
    class UssdController extends Controller
        public function test3(Request $request){
               "message" =>'We  Display This in Phone Screen',
            $jsonData =json_encode($data);// response()->json($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 => $jsonData
            curl_setopt_array( $ch, $options );
            $result =  curl_exec($ch);
    打赏 评论
  • douzhong5902 2017-01-24 05:12

    The scenario as i understood from your image and your codes is:

    1. You have a form.
    2. You filled the form and gave the post request to localhost/ussd/index.php.
    3. Your code filters the data and sends the request tohttp://localhost:7000/ussd/send with additional data.
    4. The simulator gets the data and displays.

    For this scenario you first need to have a form and i am creating forms adding Laravel Collective to use the {!! !!}.

    You also can use it by following this docs:



    {!! Form::open(['url' => 'ussd.store']) !!}
        {!! Form::input('applicationId') !!}
        {!! Form::password('password') !!}
        {!! Form::input('sessionId') !!}
        {!! Form::input('sourceAddress') !!}
    {!! Form::Close() !!}

    You also need to create the route for this action as:


    Route::resource('ussd', 'UssdController');

    I am using store method but since you are not storing the data you could send request to any post method.


    use GuzzleHttp\Client;
    class UssdController extends Controller
        public function store(Request $request)
            $data = [];
            $data["message"]='We  Display This in Phone Screen';
            $client = new Client(['base_uri' => 'http://localhost:7000/']);
            $response = $client->request('POST', 'ussd/send', $data);
            return $response->getBody();

    I suppose the http://localhost:7000 is your different application where i am sending the POST data.

    You can use GuzzleHttp to send the request to the different application. To install guzzlehttp use:

    composer require guzzlehttp/guzzle

    I don't know what you will do after send the data to the simulator, but it should work with some tweaks.

    To know more about guzzleHttp you could follow this docs:


    打赏 评论

相关推荐 更多相似问题