akalogiros
akalogiros

Reputation: 75

How to parse in array parameter from function

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.

Upvotes: 1

Views: 48

Answers (2)

Norris Oduro
Norris Oduro

Reputation: 1039

This should be

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

Upvotes: 0

Ivan Bolnikh
Ivan Bolnikh

Reputation: 752

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

Upvotes: 1

Related Questions