如何在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

Thank you everyone I found Answer

web.php like that

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

UssdController.php like that

<?php

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){

       $data=array(
           "message" =>'We  Display This in Phone Screen',
           "applicationId"=>$request->applicationId,
               "password"=>"password",
            "version"=>"1.0",
            "sessionId"=>$request->sessionId,
            "ussdOperation"=>"mt-cont",
           "destinationAddress"=>$request->sourceAddress,
            "encoding"=>"440",
            "chargingAmount"=>"5",
       );

        $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);
        Log::info($result);
        curl_close($ch);
    }
}

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:

https://laravelcollective.com/docs/5.3/html

form.blade.php

{!! 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:

routes.php

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.

UssdController.php

use GuzzleHttp\Client;

class UssdController extends Controller
{
    public function store(Request $request)
    {
        $data = [];
        $data["message"]='We  Display This in Phone Screen';
        $data["applicationId"]=$request["applicationId"];
        $data["password"]=$request["password"];
        $data["version"]="1.0";
        $data["sessionId"]=$request["sessionId"];
        $data["ussdOperation"]="mt-cont";
        $data["destinationAddress"]=$request["sourceAddress"];
        $data["encoding"]="440";
        $data["chargingAmount"]="5";

        $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:

http://docs.guzzlephp.org/en/latest/