Curl API with PHP Script

Hello everyone
I would like to create a php form to implement a configuration for my equipment.

I’m looking to transpose the curl request into php :

curl -i ‘http://genieIP:7557/devices/IDdevice/tasks?connection_request
-X POST
–data ‘{“name”:“setParameterValues”, “parameterValues”: [[“InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.SSID”, “wifiname”, “xsd:string”],[“InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.PreSharedKey.1.PreSharedKey”, “helloworld”, “xsd:string”]]}’

$url = ‘http://genieIP:7557/devices/IDdevice/tasks?connection_request’;
$data = array(“name” => “setParameterValues”,“parameterValues” => array (“InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.SSID” => “wifiname”),“InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.PreSharedKey.1.PreSharedKey” => “helloworld”));

$postdata = json_encode($data);

print_r ($postdata);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: application/json’));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);

if you have any ideas because at the moment it doesn’t work.

Best Regards

Anthony

GitHub - akinozgen/genieacs-helper-php I used this to start and had to modify it a bit. It does not guarantee it will go out and request data, but only get it from the database, and/or put it in queue for the next inbound connection.

This is most of the modifications that helped me get it working. There were more Message me for the entire file.

class ACSRequest {

    const URL = 'http://x.x.x.x:7557';

    /**
     * curl
     *
     * @param string $endpoint
     * @param string|array $query
     * @param string|array $body
     * @param string $type
     * @param string $mimeType
     * @param array $rawQuery
     * @return string
     */

// static function curl($endpoint = ‘/’, $query = ‘’, $body = ‘’, $type = ‘GET’, $mimeType = ‘application/json’, $rawQuery = null)
static function curl($endpoint = ‘/’, $query = ‘’, $body = ‘’, $type = ‘GET’, $mimeType = ‘’, $rawQuery = null)
{
if (is_array($query)) {
$query = json_encode($query);
}
$query = ($query ? ‘?query=’ . urlencode($query) : ‘’);
// $query = ($query ? ‘?query=’ . $query : ‘’);
if ($rawQuery) {
$query = ‘?’ . http_build_query($rawQuery);
}

            $curl = curl_init();

// echo "Query: " . self::URL . $endpoint . $query . “\n”;
$curl_settings = array(
CURLOPT_URL => self::URL . $endpoint . $query,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => ‘’,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $type,
);

            if ($body) {
                    $curl_settings[CURLOPT_POSTFIELDS] = is_array($body) ? json_encode($body) : $body;
            }

            if ($mimeType) {
                    $curl_settings[CURLOPT_HTTPHEADER] = array(
                            'Content-Type: '. $mimeType // application/json
                    );
            }
            curl_setopt_array($curl, $curl_settings);
            $response = curl_exec($curl);

// print_r($response);
// echo json_encode($body) . “\n”;
curl_close($curl);
return $response;
}

1 Like