skip to Main Content

I’m not a formally trained developer and I tend to just make things work for silly little projects, but I would like a few tips on how to condense some PHP code to be more compact and efficient.

At the moment, my code is as follows, it works and does what it’s supposed to, but surely there’s a better way to compose it:

<?php
function turnoff($j) {
        $url = 'http://'.$j.'/api.cgi';
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json'));
        curl_setopt($curl, CURLOPT_POSTFIELDS, '{"command":"login","data":{"username":"admin","password":"admin"}}');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $resp = curl_exec($curl);
        curl_close($curl);
        $jresp=json_decode($resp);
        $sessionID=$jresp->data->id->sessionID;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json'));
        curl_setopt($curl, CURLOPT_POSTFIELDS, '{"command":"setdatapointvalue","data":{"sessionID":"'.$sessionID.'","uid":1,"value":1}}');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $resp = curl_exec($curl);
        curl_close($curl);
}

turnoff('10.0.0.60');
?>

Basically it’s two API calls, one is to authenticate and obtain a SessionID which is then used in a second API query to set a value against it.

Thanks for any improvements offered.

2

Answers


  1. I’m not a very experienced person. One idea you can do, you write the curl call in a different function and pass the necessary parameters to it.

    Then use that function twice in this turnoff($j) function. In that way your code will be little optimized. I’m sure you have think about it also.

    My stack-overflow reputation is not enough otherwise I’ll add comment on top. Thanks in advance if it helps.

    Login or Signup to reply.
    • You can reuse the same cURL settings to make another call since everything is the same except for the request payload. You don’t have to necessarily close the current cURL and open a new one.
    • Also, using json_encode to construct JSON strings is the ideal way to create a JSON string and not hardcoding the string as JSON via trial and errors.
    • Instead of storing the current cURL response in a variable, you can simply decode the JSON only the fly and access the sessionID.
    • Closing tags in PHP are also not necessary and it’s better if you remove them. Refer this.

    Snippet:

    <?php
    
    function turnoff($j) {
            $url = 'http://'.$j.'/api.cgi';
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json'));
            curl_setopt($curl, CURLOPT_POSTFIELDS, '{"command":"login","data":{"username":"admin","password":"admin"}}');
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            $resp = curl_exec($curl);
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(['command' => 'setdatapointvalue', 'data' => ['sessionID' => json_decode($resp)->data->id->sessionID, 'uid' => 1, 'value' => 1]]));
            curl_exec($curl);
            curl_close($curl);
    }
    
    turnoff('10.0.0.60');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search