英文:
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:
<?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.
答案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:
<?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');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论