使用RESTful API(用于Rackspace的域记录API),我第一次遇到了一些问题。 也就是说,将请求作为JSON文件而不是通常的键值配对数据发送。
在我的情况下,我应该发送一个JSON格式的字符串,没有与之对应的密钥。 我在cURL中遇到的所有示例都假设请求页面需要一个。 此外,无论如何,似乎CURLOPT_POSTFIELDS选项需要一个数组(读取:键值对)。
我能够设置必要的标题(Content-Type和其他身份验证标题),但我严重依赖于在请求中添加必要的JSON字符串。
我怎样才能做到这一点?
编辑:
这是API文档:
POST https://dns.api.rackspacecloud.com/v1.0/1234/domains/2725233/records Accept: application/json X-Auth-Token: ea85e6ac-baff-4a6c-bf43-848020ea3812 Content-Type: application/json Content-Length: 725 { "records" : [ { "name" : "ftp.example.com", "type" : "A", "data" : "192.0.2.8", "ttl" : 5771 }, { "name" : "example.com", "type" : "A", "data" : "192.0.2.17", "ttl" : 86400 }, { "name" : "example.com", "type" : "NS", "data" : "dns1.stabletransit.com", "ttl" : 3600 }, { "name" : "example.com", "type" : "NS", "data" : "dns2.stabletransit.com", "ttl" : 3600 }, { "name" : "example.com", "priority" : 5, "type" : "MX", "data" : "mail.example.com", "ttl" : 3600 }, { "name" : "www.example.com", "type" : "CNAME", "comment" : "This is a comment on the CNAME record", "data" : "example.com", "ttl" : 5400 } ] }Working with a RESTful API (for Rackspace's API for domain records) and I have encountered something for the first time. That is, sending an request as a JSON file and not the usual key-value paired data.
In my case, I am supposed to send a string in JSON format, WITHOUT a key corresponding to it. All the examples I have encountered with cURL always assumes the request page expects one. Besides, it seems that the CURLOPT_POSTFIELDS option expects an array (read: key-value pair), anyway.
I am able to set the necessary headers (the Content-Type, and other authentication headers), but I am seriously stuck on putting up the necessary JSON string into the request.
How can I do this?
EDIT:
Here is the API Documentation:
POST https://dns.api.rackspacecloud.com/v1.0/1234/domains/2725233/records Accept: application/json X-Auth-Token: ea85e6ac-baff-4a6c-bf43-848020ea3812 Content-Type: application/json Content-Length: 725 { "records" : [ { "name" : "ftp.example.com", "type" : "A", "data" : "192.0.2.8", "ttl" : 5771 }, { "name" : "example.com", "type" : "A", "data" : "192.0.2.17", "ttl" : 86400 }, { "name" : "example.com", "type" : "NS", "data" : "dns1.stabletransit.com", "ttl" : 3600 }, { "name" : "example.com", "type" : "NS", "data" : "dns2.stabletransit.com", "ttl" : 3600 }, { "name" : "example.com", "priority" : 5, "type" : "MX", "data" : "mail.example.com", "ttl" : 3600 }, { "name" : "www.example.com", "type" : "CNAME", "comment" : "This is a comment on the CNAME record", "data" : "example.com", "ttl" : 5400 } ] }最满意答案
假设您正在发布文件:
$fh = fopen('/path/to/file', 'r'); $ch = curl_init($url); curl_setopt_array($ch, array( CURLOPT_INFILE => $fh, CURLOPT_POST => TRUE, //...any other options you might need )); curl_exec($ch); curl_close($ch);如果POSTing数据(如json)使用CURLOPT_POSTFIELDS
$data = '{ "foo": "bar" }'; curl_setopt_array($ch, array( CURLOPT_POSTFIELDS => $data, CURLOPT_POST => TRUE, //...any other options you might need ));Assuming you are POSTing a file:
$fh = fopen('/path/to/file', 'r'); $ch = curl_init($url); curl_setopt_array($ch, array( CURLOPT_INFILE => $fh, CURLOPT_POST => TRUE, //...any other options you might need )); curl_exec($ch); curl_close($ch);If POSTing data (like json) use CURLOPT_POSTFIELDS
$data = '{ "foo": "bar" }'; curl_setopt_array($ch, array( CURLOPT_POSTFIELDS => $data, CURLOPT_POST => TRUE, //...any other options you might need ));更多推荐
发布评论