2016-04-27 352 views
1

我有这个功能,连接到一个外部API:PHP API发送POST请求

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; 
    } 

API文档告诉我送这样的POST请求:

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 
} 

所以我尝试这样的:

$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); 

但是那让我在API日志的回应说:

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

即时通讯不知道这是什么通用的,如果有人可以帮助如何发布数据?

+5

您正在尝试发送一个PHP数组,他们告诉你发送你的数据作为JSON。见http://www.json.org – ThrowBackDewd

回答

2

将其作为JSON:

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