Add pagerule API error 1003 php

Hello everybody!
I using this php code to add page rule through API:

$data['targets'] = array(
    "target" => "url",
    "constraint" => array(
        "operator" => "matches",
        "value" => "*domain.com\/*" 
    ));
    $data['actions'] = array(
    'id' => 'SSL',
    'value' => 'flexible'

    );
    $data['status']='active';
    $data_string = json_encode($data);
    $data = send_query_cloudflare("https://api.cloudflare.com/client/v4/zones/$zone_id/pagerules","POST",$data_string);

Every time , when I try to add a new rule, I get an error.

{“success”:false,“errors”:[{“code”:1003,“message”:“Unable to parse page rule”}],“messages”:,“result”:null} 

With other requests like adding dns records everything is fine.

I tried urlencode and nothing helps.

Somebody know why?
Thanks!

constraint should be an object not an array.

image

1 Like

the same:

  $object = new stdClass();
        $object->operator = 'matches';
        $object->value = '*domain.com/*';
        $data['targets'] = array(
        "target" => "url",
        "constraint" => $object
        );
        $data['actions'] = array(
        'id' => 'SSL',
        'value' => 'flexible'
        );
        $data['status']='active';
{"success":false,"errors":[{"code":1003,"message":"Unable to parse page rule"}],"messages":[],"result":null}

Before posting the JSON print it somewhere like console and compare it with provided screenshot. It should have the same structure or API returns error.

Thanks a lot,

No square brackets:grinning:
need two array for code:

        $data["targets"] = array(array(
        "target" => "url",
        "constraint" => $object
        ));
        $data["actions"] = array(array(
        'id' => 'SSL',
        'value' => 'flexible'
        ));
$data['targets'] = array(
    array(
    "target" => "url",
    "constraint" => array(
        "operator" => "matches",
        "value" => "*domain.com/*" 
        )
    )
);
$data['actions'] = array(
    array(
    'id' => 'SSL',
    'value' => 'flexible'

    )
);
$data['status']='active';
$data_string = json_encode($data, JSON_UNESCAPED_SLASHES);

$response = send_query_cloudflare("https://api.cloudflare.com/client/v4/zones/$zone_id/pagerules","POST",$data_string);