I'm having problem with code repetition in my CURL connection class, the class is being declared as follows:
<?php
if (!defined("ROOT_PATH")) define("ROOT_PATH", dirname(__FILE__, 2));
require_once ROOT_PATH."/helpers/Helper.php";
class PhabricatorModel
{
const TOKEN = "api.token=api-...";
public function add($parameters)
{
$channel = curl_init();
curl_setopt($channel, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
curl_setopt($channel, CURLOPT_URL, "https://pb-dc.com/api/maniphest.edit");
curl_setopt($channel, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($channel, CURLOPT_POSTFIELDS, self::TOKEN.$parameters);
curl_setopt($channel, CURLOPT_POST, 1);
$result = curl_exec($channel);
if (curl_errno($channel)) {
return false;
}
curl_close($channel);
return $result;
}
public function consultProject($project)
{
$channel = curl_init();
curl_setopt($channel, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
curl_setopt($channel, CURLOPT_URL, "https://pb-dc.com/api/project.query");
curl_setopt($channel, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($channel, CURLOPT_POSTFIELDS, self::TOKEN."names[]=".$project);
curl_setopt($channel, CURLOPT_POST, 1);
$result = Helper::convert_array(json_decode(curl_exec($channel)));
if (curl_errno($channel)) {
return false;
}
curl_close($channel);
return $result;
}
public function consultStory($story)
{
$channel = curl_init();
curl_setopt($channel, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
curl_setopt($channel, CURLOPT_URL, "https://pb-dc.com/api/maniphest.query");
curl_setopt($channel, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($channel, CURLOPT_POSTFIELDS, self::TOKEN."status=status-open&fullText=".$story);
curl_setopt($channel, CURLOPT_POST, 1);
$result = Helper::convert_array(json_decode(curl_exec($channel)));
if (curl_errno($channel)) {
return false;
}
curl_close($channel);
return $result;
}
}
If I notice the same item as curl_init
in all 3 methods and I wanted to remove this repeat, not only in curl_init
but also elsewhere.