0

I'm trying to use a variable from inside an object in PHP.

I've tried to access the variable like $object->json_output but I'm receiving undefined property errors. I'm expecting to regex this output and extract data which I'll use later on.

My code is:

class curl
  {
     public function curlPut($url, $JSON, $token)
     {
        $ch = curl_init($url);
        $popt = array(
           CURLOPT_CUSTOMREQUEST => 'PUT',
           CURLOPT_RETURNTRANSFER => TRUE,
           CURLOPT_SSL_VERIFYPEER => false,
           CURLOPT_POSTFIELDS => $JSON,
           CURLOPT_HTTPHEADER => array(
              'Content-Type: application/json',
              'Authorization:'.$token.''
           ));
        curl_setopt_array($ch, $popt);
        $json_output = curl_exec($ch);
        curl_close($ch);
        return var_dump($json_output);
     }
  };

$object1 = new curl;

$object1->curlPut($url, $JSON, $token);

preg_match_all('/"id":"([0-9]*)/', $object1->json_output, $idtest);
$id_array[] = array(
  'id' => $idtest[1]
);

where $json_output is the variable I need to access and $id_array is an array of IDs I need regexed from $json_output. How would I access $json_output to be used in my preg_match_all function?

I'm new to using class/objects so apologies if this is a silly question.

Any comments would be greatly appreciated!

Sam

5
  • Instead of $json_output = ... you would need to set $this->json_output = .... Commented Apr 28, 2016 at 13:39
  • Is there any reason you're using a regular expression to read a value from JSON, rather than using json_decode()? Commented Apr 28, 2016 at 13:41
  • the common ans is stackoverflow.com/questions/4361553/… Commented Apr 28, 2016 at 13:44
  • @iainn. I'm looping through an array of JSON of variable length so I can't reliably extract what I need using json_decode. Commented Apr 28, 2016 at 13:54
  • @MohammedShafeek thank-you, I'll familiarize myself with that post Commented Apr 28, 2016 at 13:55

2 Answers 2

2

You have to set a property on your class like the following:

class Test {
  public $json_output = 'Test';
}

$test = new Test();
echo $test->json_output; // output: Test;

The property have to be public not private or protected to access outside the class.


Your Code should look like the following:

class curl {
  public $json_output = '';

  public function curlPut($url, $JSON, $token) {
    $ch = curl_init($url);
    $popt = array(
      CURLOPT_CUSTOMREQUEST => 'PUT',
      CURLOPT_RETURNTRANSFER => TRUE,
      CURLOPT_SSL_VERIFYPEER => false,
      CURLOPT_POSTFIELDS => $JSON,
      CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Authorization:'.$token.''
      ));
    curl_setopt_array($ch, $popt);
    $this->json_output = curl_exec($ch);
    curl_close($ch);
  }
}


$object1 = new curl();
$object1->curlPut($url, $JSON, $token);

preg_match_all('/"id":"([0-9]*)/', $object1->json_output, $idtest);       
$id_array[] = array('id' => $idtest[1]);
Sign up to request clarification or add additional context in comments.

Comments

1

Like this. You create class variable and then you set it using $this->json_output. After that, it is accessible via $object->json_output.

class curl
  {
     public $json_output;                            //Added by tilz0R
     public function curlPut($url, $JSON, $token)
     {
        $ch = curl_init($url);
        $popt = array(
           CURLOPT_CUSTOMREQUEST => 'PUT',
           CURLOPT_RETURNTRANSFER => TRUE,
           CURLOPT_SSL_VERIFYPEER => false,
           CURLOPT_POSTFIELDS => $JSON,
           CURLOPT_HTTPHEADER => array(
              'Content-Type: application/json',
              'Authorization:'.$token.''
           ));
        curl_setopt_array($ch, $popt);
        $this->json_output = curl_exec($ch);         //Edit by tilz0R $this-> added
        curl_close($ch);
        return var_dump($json_output);
     }
  };

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.