在PHP中将键值POST转换为数据二进制

时间:2017-01-07 22:28:03

标签: php curl

以下是我的API提供的示例:

curl 'https://api.webflow.com/collections/580e63fc8c9a982ac9b8b745/items' \
-H "Authorization: Bearer d59f681797fbb3758b2a0ce8e5f31a199e2733110cb468bb2bb0d77f23417b32" \
-H 'accept-version: 1.0.0' \
-H "Content-Type: application/json" \
--data-binary $'{
  "fields": {
    "name": "Exciting blog post title",
    "slug": "exciting-post",
    "_archived": false,
    "_draft": false,
    "color": "#a98080",
    "author": "580e640c8c9a982ac9b8b778",
    "post-body": "<p>Blog post contents...</p>",
    "post-summary": "Summary of exciting blog post",
    "main-image": "580e63fe8c9a982ac9b8b749"
  }
}'

我的网页表单通过GET / POST发布表单数据。但是,如何将表单数据转换为--data-binary中显示的内容?

以下是我在PHP中的表现:

$ch = curl_init();
$request_url = 'https://api.webflow.com/collections/' . $collection_id . '/items';

$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Bearer ' . $access_token;
$header[] = 'accept-version: 1.0.0';

curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,     json_encode($_REQUEST) );  

$rest = curl_exec($ch);

curl_close($ch);

我的json编码看起来像:

{"fields":{"region":"Saskatchewan","first-name":"Steven","last-name":"Gauerke","phone-number":"7705333580","email-address":"steven@chalamministries.com","first-name-spouse":"Daniel","last-name-spouse":"Canup","phone-number-spouse":"","email-address-spouse":"daniel@finidev.com","home-address":"Lamar Giles Rd","city":"Winder","province":"Georgia","active":"false"}}

我看到其他人都在谈论@但是在这个例子中有一个$。

0 个答案:

没有答案