1

I am using the SageOne API PHP Library. It works fine, but I get an error if I try to use get or post.

The error is,

Only variables should be passed by reference sage.api.php on line 130

My get request code is

$client = new SageOne(SAGE_CLIENT_ID, SAGE_CLIENT_SECRET);
$client->setAccessToken("c7c7547xxxxxxxxxxxx8efa4f5df08f750df");
$data = array( );
$result = "";
$client = $client->get('/products', $data);

I don’t know what’s wrong.

Full Code

require 'sage.api.php';
define('SAGE_CLIENT_ID', "fa1e8c1b114347a356d2");
define('SAGE_CLIENT_SECRET', "faaa7b353521f823ba13e3a20e72dd057c3a5fd1");

$client = new SageOne(SAGE_CLIENT_ID, SAGE_CLIENT_SECRET);
$callbackURL = 'xxxxx/addonmodules.php?module=sageone';
// We need to build the authorise url and redirect user to authorise our app
if(!$_GET['code']){

    $authoriseURL = $client->getAuthoriseURL($callbackURL);

    // redirect user
    header("Location: ".$authoriseURL);


    exit;


// We now have the authorisation code to retrieve the access token
} else {

$accessToken = $client->getAccessToken($_GET['code'], $callbackURL);

$token= $accessToken['access_token'];
$end = 'public';
$data ='';
$result = $client->get($end, $data);
echo '<pre>';
print_r($result);

Code Snippets from sage.api.php

    class SageOne { ...

...
public function get($endpoint, $data=false){
        return $this->call($endpoint, 'get', $data);
    }
...

// error line 130 from this code

    private function buildSignature($method, $url, $params, $nonce){

        // uc method and append &
        $signature = strtoupper($method).'&';

        // percent encode bit of url before ? and append &
        $signature .= rawurlencode(array_shift(explode('?', $url))).'&';

        // percent encode any params and append &
        if (is_array($params)){

            // sort params alphabetically
            $this->ksortRecursive($params);

            // build query string from params, encode it and append &
            $signature .= str_replace(
                array('%2B'), 
                array('%2520'), 
                rawurlencode(http_build_query($params, '', '&'))
            ).'&';

        // params can be string
        } else {

            // build query string from params, encode it and append &
            $signature .= rawurlencode($params).'&';
        }

        // add 'nonce' - just use an md5
        $signature .= $nonce;

        // now generate signing key
        $signingKey = rawurlencode($this->signingSecret).'&'.rawurlencode($this->accessToken);

        // encode using sha 1, then base64 encode       
        $finalSignature = base64_encode(hash_hmac('sha1', $signature, $signingKey, true));

        return $finalSignature;

    }

This is the shortest i can make to see all important code

Christoph Petschnig
  • 4,047
  • 1
  • 37
  • 46
arcronis
  • 61
  • 1
  • 7

2 Answers2

0

This is due to trying to return the result of a function or method directly to another function or method... the result doesn't have a reference.

So, for example:

$obj->method(doSomething(), 'asdf', 'qwerty');

The error means you should assign the value of doSomething() before passing it.

$result = doSomething();
$obj->method($result, 'asdf', 'qwerty');

Also see: Only variables should be passed by reference

Community
  • 1
  • 1
Rob W
  • 9,134
  • 1
  • 30
  • 50
0

A function (in this case, $client->get()) can be defined to receive its parameters by reference. This means that it can modify those parameters directly. So if you call $client->get($a, $b), the function may alter the values of $a and $b.

Clearly, it can only alter the values of variables, so when a function receives a parameter by reference, you must pass it a variable, not a string, an integer, or a direct call to another function.

So if the function $client->get() receives its first parameter by reference, none of the following can work:

$client->get('string', $data);
$client->get(15, $data); // int
$client->get(other_function_call(), $data);
$client->get(12.5, $data); // float
$client->get(array(), $data);

You have to do this:

$a = 'string';
$client->get($a, $data);

Or $a = whatever, be it a string, an int, a function call. The point is (and this is stated quite clearly in the error message) that you must pass a variable. So save whatever you want to pass as a variable, then pass that.

TRiG
  • 10,148
  • 7
  • 57
  • 107
  • i tried now $end = "/public"; $data = ""; $client2->get($end, $data); but same error ... – arcronis Jul 23 '15 at 18:34
  • That seems odd to me, @arcronis. Are you sure the error is being created by the same code. Try creating a *minimal working example* (i.e., a complete piece of code which is as short & simple as it can be), and adding it to your question. – TRiG Jul 23 '15 at 18:52
  • i added more code i think so you can see whats important – arcronis Jul 23 '15 at 19:00
  • Hmm. `$client->get()` does not seem to receive its parameters by reference (if it did, it would be `public function get(&$endpoint, &$data)`, but there is no `&`). That suggests that the problem lies elsewhere. Your error message mentions a line number. Which line is that? – TRiG Jul 23 '15 at 19:07
  • Its line 130 in my snippet the line rawurlencode in buildsignature function – arcronis Jul 23 '15 at 19:11