1

The API I'm requesting is expecting a GET HTTP method

I want to avoid something like

domain.com/api/v1/module/user?access_token=er78e9wr7fasf7s9a8fsd7fa8f7sa8f for example.

I'd rather be able to request

domain.com/api/v1/module/user and set the access_token value in the header, I'm aware you can do this with post, but I'd like to do this for a GET method.

I currently have the following

    public function fetch() 
    {
        $response = json_decode(
            $this->prepareRequest(
                new \Web\Curl\Wrapper,
                $this->prepareRequestURL()
            )->execute()
        );
    }
    private function prepareRequestURL()
    {
        return $this->apiConfig->getBaseDomain() . $this->apiConfig->getUserSelfURL();
    }

    private function prepareRequest(\Web\Curl\Wrapper $curlWrapper, $url)
    {
        $curlWrapper->init($url);
        $curlWrapper->setOption(CURLOPT_RETURNTRANSFER, 1);
        $curlWrapper->setOption(CURLOPT_HTTPHEADER, array(
            $this->apiConfig->getAccessTokenKey() => $_SESSION[$this->apiConfig->getAccessTokenKey()]
        ));
        return $curlWrapper;
    }

My curl wrapper class

private $curl;

public function init($requestLocation = null)
{
    $this->curl = curl_init($requestLocation);
}

public function preparePost($postData)
{
    $this->setOption(CURLOPT_POST, 4);
    $this->setOption(CURLOPT_POSTFIELDS, $postData);
    $this->setOption(CURLOPT_SSL_VERIFYPEER, false);
    $this->setOption(CURLOPT_RETURNTRANSFER, true);
}

public function execute()
{
    return curl_exec($this->curl);
}

public function setOption($option, $value)
{
    curl_setopt($this->curl, $option, $value);
}
3
  • Can you please show something you already have tried? Commented Apr 13, 2016 at 8:40
  • No, as if I knew what to try then I'd have done it - I've got it successfully working with the query string appended to the URL but I want to avoid this hard-coding. @C0dekid.php Commented Apr 13, 2016 at 8:41
  • Maybe you should take a look at this: stackoverflow.com/questions/25646692/… I don't know if it's possible to achieve what you want. Commented Apr 13, 2016 at 8:45

1 Answer 1

1

...and set the access_token value in the header, I'm aware you can do this with post, but I'd like to do this for a GET method.

HTTP headers are set in the same way for any type of request (POST, GET etc).

The issue with your code is that you set headers in wrong way. CURLOPT_HTTPHEADER option expects numeric-indexed array, not associative array. Furthermore each array element should be a string in the format "key: value".

So you have to do such changes in your prepareRequest method:

...
$key = $this->apiConfig->getAccessTokenKey();
$value = $_SESSION[$token];
$curlWrapper->setOption(CURLOPT_HTTPHEADER, array(
    "$key: $value"
));
...
Sign up to request clarification or add additional context in comments.

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.