1

I would like to ask how we can put into an array a variable reference from the current object.

In my case i have an array used for curl post and some fields are static (grant type & scope) while others (id & secret) are dynamic.

More specifivally i want to put the id & the secret as shown below:

  $headers = [
    'client_id='=> $this->id,
    'client_secret='=>$this->secret,
    'grant_type='=>'client_credentials', 
    'scope='=>'public' 
];
  ....
  ....
  curl_setopt($s, CURLOPT_HTTPHEADER, $headers);

I am getting a "PHP Fatal error: Constant expression contains invalid operations in ..." for the second & third row.

2
  • how are you getting the id and secret and are you sure they are set before calling the curl_setopt($s, CURLOPT_HTTPHEADER, $headers);? Commented Oct 14, 2017 at 17:30
  • Give more context. Are you declaring $headers as an instance property? Commented Oct 14, 2017 at 17:42

2 Answers 2

1

For headers you should use something like

array('Content-type: text/plain', 'Content-length: 100')

not associative array. See http://php.net/manual/en/function.curl-setopt.php

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

Comments

0

This should be

$headers = [
    "client_id: {$this->id}",
    "client_secret: {$this->secret}",
    "grant_type: client_credentials", 
    "scope: public" 
];
  ....
  ....
  curl_setopt($s, CURLOPT_HTTPHEADER, $headers);

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.