使用 CloudFlare API 通过 PHP/Curl 返回错误
Using CloudFlare API via PHP/Curl returning error
我正在通过 PHP 脚本更新我的 DNS。我查看了与 cURL 相关的 API 文档,因此我尝试将 cURL post 转换为 PHP.
我有以下代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/<MY_ZONE>/dns_records/<MY_ID>");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$fields = array();
$fields["X-Auth-Email"] = "someone@mydomain.com";
$fields["X-Auth-Key"] = "MY_KEY";
$fields["Content-Type"] = "application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $fields);
$dnsData = array();
$dnsData["id"] = "MY_ID";
$dnsData["type"] = "A";
$dnsData["name"] = "home";
$dnsData["content"] = $newIPAddress;
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($dnsData));
echo "posting to API<br />";
$result = curl_exec($ch);
echo "Result: " . $result;
通过上面的代码,我从 Cloudflare 得到了以下响应。
{"success":false,"errors":[{"code":6003,"message":"Invalid request
headers","error_chain":[{"code":6100,"message":"Missing X-Auth-Email
header"},{"code":6101,"message":"Missing X-Auth-Key
header"},{"code":6105,"message":"Invalid Content-Type header, valid
values are
application/json,multipart/form-data"}]}],"messages":[],"result":null}
我试过将 json_encode 更改为 http_build_query,但两者 return 都是同样的错误。
我认为你误用了 curl_setopt。
这是设置多个的正确方法header:
curl_setopt($ch,CURLOPT_HTTPHEADER, ['HeaderName: HeaderValue','HeaderName2: HeaderValue2']);
编辑
为了更清楚:
$headers = [
'X-Auth-Email: someone@mydomain.com',
'X-Auth-Key: MY_KEY',
'Content-Type: application/json'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
Headers 不是 key/value 对,而只是值。
此外,您应该使用 http_build_query() 发送 POST 数据。
问题是您在向它传递表单数据时告诉它使用 application/json
。将您的 Content-type 设置为 multipart/form-data
它应该可以工作。如果您想要一个如何使用 JSON API 的示例,请告诉我。
EDIT: CURLOPT_HTTPHEADER does not accept key/values, only values. More information
$fields["X-Auth-Email"] = "someone@mydomain.com";
因此需要改为:
$fields[]= "X-Auth-Email: someone@mydomain.com";
我已经测试了这段代码,我认为你可以毫无问题地使用它。
示例:
$request_headers = array(
'X-Auth-Email: user@emailaddress.com',
'X-Auth-Key: d820fa8fc881921323e08a2c19b8347896ac26',
'Content-Type: application/json'
);
然后:
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
我正在通过 PHP 脚本更新我的 DNS。我查看了与 cURL 相关的 API 文档,因此我尝试将 cURL post 转换为 PHP.
我有以下代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/<MY_ZONE>/dns_records/<MY_ID>");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$fields = array();
$fields["X-Auth-Email"] = "someone@mydomain.com";
$fields["X-Auth-Key"] = "MY_KEY";
$fields["Content-Type"] = "application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $fields);
$dnsData = array();
$dnsData["id"] = "MY_ID";
$dnsData["type"] = "A";
$dnsData["name"] = "home";
$dnsData["content"] = $newIPAddress;
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($dnsData));
echo "posting to API<br />";
$result = curl_exec($ch);
echo "Result: " . $result;
通过上面的代码,我从 Cloudflare 得到了以下响应。
{"success":false,"errors":[{"code":6003,"message":"Invalid request headers","error_chain":[{"code":6100,"message":"Missing X-Auth-Email header"},{"code":6101,"message":"Missing X-Auth-Key header"},{"code":6105,"message":"Invalid Content-Type header, valid values are application/json,multipart/form-data"}]}],"messages":[],"result":null}
我试过将 json_encode 更改为 http_build_query,但两者 return 都是同样的错误。
我认为你误用了 curl_setopt。
这是设置多个的正确方法header:
curl_setopt($ch,CURLOPT_HTTPHEADER, ['HeaderName: HeaderValue','HeaderName2: HeaderValue2']);
编辑
为了更清楚:
$headers = [
'X-Auth-Email: someone@mydomain.com',
'X-Auth-Key: MY_KEY',
'Content-Type: application/json'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
Headers 不是 key/value 对,而只是值。
此外,您应该使用 http_build_query() 发送 POST 数据。
问题是您在向它传递表单数据时告诉它使用 application/json
。将您的 Content-type 设置为 multipart/form-data
它应该可以工作。如果您想要一个如何使用 JSON API 的示例,请告诉我。
EDIT: CURLOPT_HTTPHEADER does not accept key/values, only values. More information
$fields["X-Auth-Email"] = "someone@mydomain.com";
因此需要改为:
$fields[]= "X-Auth-Email: someone@mydomain.com";
我已经测试了这段代码,我认为你可以毫无问题地使用它。 示例:
$request_headers = array(
'X-Auth-Email: user@emailaddress.com',
'X-Auth-Key: d820fa8fc881921323e08a2c19b8347896ac26',
'Content-Type: application/json'
);
然后:
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);