0

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

4
  • I tried like that Commented Jan 24, 2017 at 3:04
  • 1
    How is laravel related to this question? Commented Jan 24, 2017 at 3:10
  • request()->json()->all() is the way to extract json from request Commented Jan 24, 2017 at 3:27
  • 1
    It will benefit yourself and the community if you take a second to show what you have done, what is working and what isn't working. stackoverflow.com/help/how-to-ask Commented Jan 24, 2017 at 3:50

2 Answers 2

1

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/

Sign up to request clarification or add additional context in comments.

8 Comments

Thank You for your answer, But It show some error ServerException in RequestException.php line 111: Server error: POST http://localhost:7000/ussd/send resulted in a 500 Server Error response:
@LilanthaRajapaksha could you explain me more about your localhost:7000 server. How does it works? or have you checked is ussd/send url is working? 500 Server Error means there is something issue with your server i.e. your requesting server. Also check if your url accepts POST request.
I can sent small demo video, Its okkk
@LilanthaRajapaksha Sure will help you. But i might not help more if there is some issue with your server you are requesting.
Sorry, Could you add your video in some place from where i could have a look at your issue.
|
0

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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.