1

I have already written an application in a procedural way and am trying to move into into a Laravel framework. I'm having trouble with the SOAP exchange section as I am getting an ID value that authenticates the user but cannot access that value (as a cookie) later in the program to authenticate the search.

Here is my code so far:

<?php namespace App;

use Artisaninweb\SoapWrapper\Facades\SoapWrapper;
use Illuminate\Http\RedirectResponse;

class SoapController {

private $auth_response;
private $cookie;
private $search_client;
private $search_response;

public function soapExchange() {

    // create SOAP client and add service details
    SoapWrapper::add(function ($service) {

        $service
            ->name('WoSAuthenticate')
            ->wsdl('http://search.webofknowledge.com/esti/wokmws/ws/WOKMWSAuthenticate?wsdl')
            ->trace(true)
            ->cache(WSDL_CACHE_NONE);
    });

    SoapWrapper::service('WoSAuthenticate', function($service) {
        // call authenticate() method to get SID cookie
        $auth_response = $service->call('authenticate', []);
        $cookie = $auth_response->return;
        // test for cookie return
        // print($cookie);
    });

    // create SOAP client and add service details
    $search_client = new SoapWrapper;
    $search_client::add(function ($service) {

        $service
            ->name('WoSSearch')
            ->wsdl('http://search.webofknowledge.com/esti/wokmws/ws/WokSearch?wsdl')
            ->trace(true)
            ->cache(WSDL_CACHE_NONE);
    });

    if (isset($auth_response->return)) {

        // if there is an SID returned then add it to the cookie attribute of the search client
        $search_client->__setCookie('SID', $cookie);
    } else {
        // route to relevant view to display throttle error
        return redirect('throttle');
    }
}
}

I am successfully retrieving the response from the Web API call and getting a code to authenticate the user, saved as $cookie. However, I need then to create another SoapWrapper for performing the search and this needs the ID code attached by using the __setCookie method. If nothing is returned by the authenticate call then it redirects to an error message via throttle.blade.php elsewhere.

Surely there is a way to return a value created from a function so that it can be used elsewhere?

** EDIT **

Looked into employing SoapClient instead and including all operations within a single function. It all relates to a specific Web API anyway so I guess separation of concerns is not so much of an issue. FYI the new class I am trying is this:

<?php namespace App\Models;

use SoapClient;
use Illuminate\Http\RedirectResponse;

class SoapWrapper {

public function soapExchange() {

    // set WSDL for authentication and create new SOAP client
    $auth_url  = "http://search.webofknowledge.com/esti/wokmws/ws/WOKMWSAuthenticate?wsdl";

    // array options are temporary and used to track request & response data
    $auth_client = @new SoapClient($auth_url);

    // set WSDL for search and create new SOAP client
    $search_url = "http://search.webofknowledge.com/esti/wokmws/ws/WokSearch?wsdl";

    // array options are temporary and used to track request & response data
    $search_client = @new SoapClient($search_url);

    // run 'authenticate' method and store as variable
    $auth_response = $auth_client->authenticate();

    // call 'setCookie' method on '$search_client' storing SID (Session ID) as the response (value) given from the 'authenticate' method
    // check if an SID has been set, if not it means Throttle server has stopped the query, therefore display error message
    if (isset($auth_response->return)) {
        $search_client->__setCookie('SID',$auth_response->return);
    } else {
        return Redirect::route('throttle');
    }
}
}    
3
  • 3
    what about the "use" operator in anonymous functions - is this something you can work with? Usage: $two = function() use ($result) { var_dump($result); }; Commented May 27, 2015 at 13:47
  • I did look into that but it seems that you can only use that method if the function where you put the use operator is nested within the function containing the variable. Not sure if that will work but I'll give it a try... Commented May 27, 2015 at 13:49
  • 1
    you can use global variable see manual php.net/manual/fr/reserved.variables.globals.php Commented May 27, 2015 at 13:53

2 Answers 2

3

Maybe try $GLOBALS?

<?php
$GLOBALS[data] = "something";

function abc(){
   echo $GLOBALS[data];
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

I thought it was a bit of a security risk to use global variables?
1
use Artisaninweb\SoapWrapper\Facades\SoapWrapper;    


class SoapController extends Controller {

    public $resultSoapStatus;
    public $resultSoapAuthority;

    public function heySoap{

        SoapWrapper::add(function ($service) ...

        $data = [
                'MerchantID'       => $MerchantID,
                'Amount'           => $Amount,
                'Description'      => $Description,
                'Email'            => $Email,
                'Mobile'           => $Mobile,
                'CallbackURL'      => $CallbackURL
            ];

        SoapWrapper::service('test', function ($service) use ($data) {
          $resultSoap = $service->call('PaymentRequest', [$data]);
          $this->resultSoapStatus    = $resultSoap->Status;
          $this->resultSoapAuthority = $resultSoap->Authority;
             });

         if($this->resultSoapStatus == 100 && strlen($this->resultSoapAuthority) == 36)
          {
           //Do Something
          }
          else
          {
            return Redirect::back();
          }


      }
}

Enjoy bro

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.