anione828
By:
anione828

How to hide result of reboot curl method

March 6, 2016 669 views
API PHP

i was made a script php to reboot a droplets. But when run, this script also display the result of this script. How to hide?

public function rebootDroplet($id) {
        $this->id = $id;
        $this->data = array("type" => "reboot");
        $this->data_string = json_encode($this->data);

        $this->ch = curl_init('https://api.digitalocean.com/v2/droplets/'.$this->id.'/actions');
        curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->data_string);
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, array(
            'Authorization: Bearer '.$this->api,
            'Content-Type: application/json',
            'Content-Length: ' . strlen($this->data_string))
        );

        $this->result = curl_exec($this->ch);
        return $this->result = true;
    }
1 Answer

Hi!

Based on the code you have provided here, the option you need to set is CURLOPT_RETURNTRANSFER. This will prevent curl from displaying the result it is returned. If you use the following configuration, do you receive the desired output?

public function rebootDroplet($id) {
$this->id = $id;
$this->data = array("type" => "reboot");
$this->datastring = jsonencode($this->data);

    $this->ch = curl_init('https://api.digitalocean.com/v2/droplets/'.$this->id.'/actions');
    curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->data_string);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($this->ch, CURLOPT_HTTPHEADER, array(
        'Authorization: Bearer '.$this->api,
        'Content-Type: application/json',
        'Content-Length: ' . strlen($this->data_string))
    );

    $this->result = curl_exec($this->ch);
    return $this->result = true;
}

Hopefully this helps!

DigitalOcean Support

Have another answer? Share your knowledge.