Laravel-如何传递发布请求的结果

时间:2019-07-04 12:37:05

标签: laravel

我有我编写并测试过的代码。第一阶段

  

mokkle_test(请求$ request)

正在工作。我对如何将请求的结果传递到第二阶段有疑问

第一阶段:

 public function mokkle_test(Request $request)
{
$telco_match=['name'=>'Icell'];
$telco=Telco::where($telco_match)->first(); 
try{
            $client = new Client();
            $response = $client->request(
                    'POST', $telco->send_call, [
                    'json' => [
                    'msisdn' => $request->msisdn,
                    'username' => $telco->username,
                    'password' => $telco->cpPwd,
                    'text' =>$request->text,
                    'correlator' =>$request->correlator,
                    'serviceid' =>$request->serviceid,
                    'shortcode' => $request->shortcode
                ],
                'headers' => [

                    'Accept' => 'application/json',
        'Content-Type' => 'application/json'
                ],
                ]
            );


    $noti=new Notification_log();
    $noti->error_message= (string)$response->getBody();

    $noti->save();

            $status = $response->getStatusCode();
             $result = $response->getBody();

             return  $result;
}catch(\Exception $e)
{

  return $e->getMessage();
}      

}

...并且效果很好。

如何将响应结果传递到下面显示的另一个函数中。

第二阶段:

  function subscribe($request,$telco)
 {   
 try{
            $client = new Client();
            $response = $client->request(
                    'POST', $telco->billing_callback_2, [
                    'json' => [
                    'msisdn' => $request->msisdn,
                    'username' => $telco->username,
                    'password' => $telco->password,
                    'amount' =>$request->amount,
                    'shortcode' => $request->shortcode
                ],
                'headers' => [
                    'auth' => $telco->authorization,
                    'key' => $telco->key,
                    'Accept' => 'application/json',
        'Content-Type' => 'application/json'
                ],
                ]
            );

    $amount = $request->amount;
    $shortcode = $request->shortcode;

    $noti=new Notification_log();
    $noti->error_message=(string)$response;
    $noti->msisdn=$request->msisdn;
    $noti->product_id=$request->productid;
    $noti->save();

            $status = $response->getStatusCode();
             $result = $response->getBody();
             $request = array();
             $request->text= "Weldone";
             $request->amount = $amount;
             $request->serviceid="100010";
             $request->correlator="876543ghj";
             $result_sms=self::mokkle_test($request); 
             return  $result;
}catch(\Exception $e)
{    
  return $e;
}                   
}

我尝试过,但是什么也没发生

  

$ result_sms = self :: mokkle_test($ request);

请协助。我如何实现我的目标。请帮助我。

1 个答案:

答案 0 :(得分:0)

在这里您可以将其传递给其他方法

public function mokkle_test(Request $request)
{
    $telco_match = ['name' => 'Icell'];
    $telco       = Telco::where($telco_match)->first();
    try {
        $client   = new Client();
        $response = $client->request(
            'POST', $telco->send_call, [
                'json'    => [
                    'msisdn'     => $request->msisdn,
                    'username'   => $telco->username,
                    'password'   => $telco->cpPwd,
                    'text'       => $request->text,
                    'correlator' => $request->correlator,
                    'serviceid'  => $request->serviceid,
                    'shortcode'  => $request->shortcode
                ],
                'headers' => [

                    'Accept'       => 'application/json',
                    'Content-Type' => 'application/json'
                ],
            ]
        );
        // Here you can pass it to the other method
        this.subscribe($response, $telco); // <--- $response will be your "$request" parameter

        $noti                = new Notification_log();
        $noti->error_message = (string)$response->getBody();

        $noti->save();

        $status = $response->getStatusCode();
        $result = $response->getBody();

        return $result;
    } catch (\Exception $e) {

        return $e->getMessage();
    }

}