0

I am trying to build a web-gui for jaredhendrickson13/pfsense-api (https://github.com/jaredhendrickson13/pfsense-api) Either I can't get array indexes on return or they not there.

I am using laravel8 with php 7.4

My code to get responses;

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://192.168.1.175/api/v1/firewall/rule",
    CURLOPT_TIMEOUT => 30000,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_SSL_VERIFYPEER => false, //add line
    CURLOPT_SSL_VERIFYHOST => false, //add line
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_USERPWD => "$username:$password",
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
        // Set Here Your Requesred Headers
        'Content-Type: application/json',
    ),
));

$response = curl_exec($curl);
curl_close($curl);
$object = json_decode($response,true);

return $object;

And the output I am gettins is;

{
    "status": "ok",
    "code": 200,
    "return": 0,
    "message": "Success",
    "data": [
        {
            "id": "",
            "tracker": "1665883545",
            "type": "pass",
            "interface": "wan",
            "ipprotocol": "inet",
            "tag": "",
            "tagged": "",
            "max": "",
            "max-src-nodes": "",
            "max-src-conn": "",
            "max-src-states": "",
            "statetimeout": "",
            "statetype": "keep state",
            "os": "",
            "protocol": "tcp",
            "source": {
                "any": ""
            },
            "destination": {
                "address": "192.168.1.201"
            },
            "descr": "Test2",
            "created": {
                "time": "1665883545",
                "username": "admin@192.168.1.100 (Local Database)"
            },
            "updated": {
                "time": "1665883556",
                "username": "admin@192.168.1.100 (Local Database)"
            }
        },
        {
            "id": "",
            "tracker": "1665883538",
            "type": "pass",
            "interface": "wan",
            "ipprotocol": "inet",
            "tag": "",
            "tagged": "",
            "max": "",
            "max-src-nodes": "",
            "max-src-conn": "",
            "max-src-states": "",
            "statetimeout": "",
            "statetype": "keep state",
            "os": "",
            "protocol": "tcp",
            "source": {
                "any": ""
            },
            "destination": {
                "address": "192.168.1.202"
            },
            "descr": "Test1",
            "created": {
                "time": "1665883538",
                "username": "admin@192.168.1.100 (Local Database)"
            },
            "updated": {
                "time": "1665883566",
                "username": "admin@192.168.1.100 (Local Database)"
            }
        },
        {
            "type": "pass",
            "ipprotocol": "inet",
            "descr": "Default allow LAN to any rule",
            "interface": "lan",
            "tracker": "0100000101",
            "source": {
                "network": "lan"
            },
            "destination": {
                "any": ""
            }
        },
        {
            "type": "pass",
            "ipprotocol": "inet6",
            "descr": "Default allow LAN IPv6 to any rule",
            "interface": "lan",
            "tracker": "0100000102",
            "source": {
                "network": "lan"
            },
            "destination": {
                "any": ""
            }
        }
    ]
}

Below is the response from postman which is same with my output;

{
"status": "ok",
"code": 200,
"return": 0,
"message": "Success",
"data": [
    {
        "id": "",
        "tracker": "1665883545",
        "type": "pass",
        "interface": "wan",
        "ipprotocol": "inet",
        "tag": "",
        "tagged": "",
        "max": "",
        "max-src-nodes": "",
        "max-src-conn": "",
        "max-src-states": "",
        "statetimeout": "",
        "statetype": "keep state",
        "os": "",
        "protocol": "tcp",
        "source": {
            "any": ""
        },
        "destination": {
            "address": "192.168.1.201"
        },
        "descr": "Test2",
        "created": {
            "time": "1665883545",
            "username": "admin@192.168.1.100 (Local Database)"
        },
        "updated": {
            "time": "1665883556",
            "username": "admin@192.168.1.100 (Local Database)"
        }
    },
    {
        "id": "",
        "tracker": "1665883538",
        "type": "pass",
        "interface": "wan",
        "ipprotocol": "inet",
        "tag": "",
        "tagged": "",
        "max": "",
        "max-src-nodes": "",
        "max-src-conn": "",
        "max-src-states": "",
        "statetimeout": "",
        "statetype": "keep state",
        "os": "",
        "protocol": "tcp",
        "source": {
            "any": ""
        },
        "destination": {
            "address": "192.168.1.202"
        },
        "descr": "Test1",
        "created": {
            "time": "1665883538",
            "username": "admin@192.168.1.100 (Local Database)"
        },
        "updated": {
            "time": "1665883566",
            "username": "admin@192.168.1.100 (Local Database)"
        }
    },
    {
        "type": "pass",
        "ipprotocol": "inet",
        "descr": "Default allow LAN to any rule",
        "interface": "lan",
        "tracker": "0100000101",
        "source": {
            "network": "lan"
        },
        "destination": {
            "any": ""
        }
    },
    {
        "type": "pass",
        "ipprotocol": "inet6",
        "descr": "Default allow LAN IPv6 to any rule",
        "interface": "lan",
        "tracker": "0100000102",
        "source": {
            "network": "lan"
        },
        "destination": {
            "any": ""
        }
    }
]

}

How can I extract "data" part to put into a foreach loop?

It look likes the response is kinda broken but I am not able to decide if I am doing it wrong or not.

Thanks!

zehyr
  • 41
  • 5

1 Answers1

0

Changing from raw curl to Http class worked.

zehyr
  • 41
  • 5