寻找关于如何编写更简洁的PHP代码的提示。

huangapple go评论58阅读模式
英文:

Looking for tips on how to make more concise PHP code

问题

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.

英文:

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:

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

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

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

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

turnoff(&#39;10.0.0.60&#39;);
?&gt;

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.

答案1

得分: 0

你可以这样做,将curl调用写在一个不同的函数中,并传递所需的参数给它。

然后在这个turnoff($j)函数中两次使用该函数。这样你的代码会稍微优化一些。我相信你也已经考虑过这个问题了。

英文:

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.

答案2

得分: 0

  • 可以重用相同的 cURL 设置来进行另一个调用,因为除了请求有效载荷以外,其他都相同。你不一定需要关闭当前的 cURL 并打开一个新的。
  • 同样,使用 json_encode 构建 JSON 字符串是创建 JSON 字符串的理想方式,而不是通过试错方式硬编码字符串。
  • 不必将当前的 cURL 响应存储在变量中,可以直接解码 JSON 并访问 sessionID
  • 在 PHP 中关闭标签也是不必要的,最好将其移除。参考 this
英文:
  • 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:

&lt;?php

function turnoff($j) {
        $url = &#39;http://&#39;.$j.&#39;/api.cgi&#39;;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(&#39;Accept: application/json&#39;));
        curl_setopt($curl, CURLOPT_POSTFIELDS, &#39;{&quot;command&quot;:&quot;login&quot;,&quot;data&quot;:{&quot;username&quot;:&quot;admin&quot;,&quot;password&quot;:&quot;admin&quot;}}&#39;);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $resp = curl_exec($curl);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode([&#39;command&#39; =&gt; &#39;setdatapointvalue&#39;, &#39;data&#39; =&gt; [&#39;sessionID&#39; =&gt; json_decode($resp)-&gt;data-&gt;id-&gt;sessionID, &#39;uid&#39; =&gt; 1, &#39;value&#39; =&gt; 1]]));
        curl_exec($curl);
        curl_close($curl);
}

turnoff(&#39;10.0.0.60&#39;);

huangapple
  • 本文由 发表于 2023年6月8日 11:42:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定