简体   繁体   中英

PHP API sending POST requests

I have this function that connects to an external API:

function ICUK_Request($url, $method = 'GET', $body = NULL) {
        $client = new IcukApiClient();
        $client->username = "user";
        $client->key = "pass";
        $client->encryption = "SHA-512";

        $req = new IcukApiRequest();
        $req->url = $url;
        $req->method = $method;

        if(!is_null($body)) {
            $req->body = $body;
        }

        $res = $client->send($req);
        if ($res->success) {
            $obj = json_decode($res->response);
        }
        else {
            throw new Exception('There was an error contacting the API.');
        }

        return $obj;
    }

the API docs are telling me to send the POST request like this:

POST /domain/registration/new/
{
   "domain_name": "domain.co.uk",
   "hosting_type": "NONE",
   "registration_length": 1,
   "auto_renew": false,
   "domain_lock": false,
   "whois_privacy": false,
   "contact_registrant_id": 1
}

so i tried this:

$arr = array(
    "domain_name" => "domain.co.uk",
   "hosting_type" => "NONE",
   "registration_length" => 1,
   "auto_renew" => false,
   "domain_lock" => false,
   "whois_privacy" => false,
   "contact_registrant_id" => 1
);

ICUK_Request("/domain/registration/new", "POST", $arr);

but thats giving me a response in the API Logs saying:

{
  "exception_message": "Internal API exception occured",
  "exception_type": "InternalApiException"
}

im not sure if this would be anything generic and if anyone can help on how to POST the data?

Send it as json:

$values = json_encode($arr);
ICUK_Request("/domain/registration/new", "POST", $values);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM