使用PHP cURL发送非键值对POST请求?(Using PHP cURL to send a non key

编程入门 行业动态 更新时间:2024-10-25 08:14:50
使用PHP cURL发送非键值对POST请求?(Using PHP cURL to send a non key-value pair POST request?)

使用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 ));

更多推荐

本文发布于:2023-08-06 18:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1453786.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:键值   cURL   PHP   key   send

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!