在 PHP 中使用 CURL 在 Netlify 上创建站点
Creating Site on Netlify using CURL in PHP
因此,我目前能够使用 curl 命令来获取有关我在 netlify 上的站点的信息。但是,根据 API 文档,我应该也可以使用 POST 创建站点。对于我的生活,我似乎无法弄清楚发生了什么。当 运行 此页面思考了很长时间,然后刷新并 returns 一个空的响应,并且没有创建站点。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.netlify.com/api/v1/sites");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "User-Agent: AppName (accountEmail)";
$headers[] = "Content-Length: 1000";
$headers[] = "Authorization: Bearer MyAPIAuthKey";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
print $result;
这是 Netlify API 文档,专门引用 "Create Site" 部分(第 3 下)https://www.netlify.com/docs/api/#sites
知道我在这里遗漏了什么吗?就像我说的,我授权正确并且可以从我的帐户中获取我想要的所有数据。我似乎什么都做不了。POST
************************************************ **** 更新 ********************************************* ****
我正在传递一些属性,但是请求超时。
curl_setopt($ch, CURLOPT_URL, "https://api.netlify.com/api/v1/sites");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"name\":\"awb-test\"");
curl_setopt($ch, CURLOPT_TIMEOUT, 10000);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "User-Agent: AWB (myemail@)";
$headers[] = "Content-Type: application/json";
$headers[] = "Content-Length: 1000";
$headers[] = "Authorization: Bearer MyAPIkey";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
正如傻瓜在 OP 下的评论中所述,问题在于硬编码的内容长度。为了更正,我构建了一个 POST 数据数组,json 对其进行编码并使用字符串长度传递适当的值。
$post_data = array(
'name' => '',
'force_ssl' => true,
'repo' => '',
);
$post_data = json_encode($post_data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
因此,我目前能够使用 curl 命令来获取有关我在 netlify 上的站点的信息。但是,根据 API 文档,我应该也可以使用 POST 创建站点。对于我的生活,我似乎无法弄清楚发生了什么。当 运行 此页面思考了很长时间,然后刷新并 returns 一个空的响应,并且没有创建站点。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.netlify.com/api/v1/sites");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "User-Agent: AppName (accountEmail)";
$headers[] = "Content-Length: 1000";
$headers[] = "Authorization: Bearer MyAPIAuthKey";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
print $result;
这是 Netlify API 文档,专门引用 "Create Site" 部分(第 3 下)https://www.netlify.com/docs/api/#sites
知道我在这里遗漏了什么吗?就像我说的,我授权正确并且可以从我的帐户中获取我想要的所有数据。我似乎什么都做不了。POST
************************************************ **** 更新 ********************************************* ****
我正在传递一些属性,但是请求超时。
curl_setopt($ch, CURLOPT_URL, "https://api.netlify.com/api/v1/sites");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"name\":\"awb-test\"");
curl_setopt($ch, CURLOPT_TIMEOUT, 10000);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "User-Agent: AWB (myemail@)";
$headers[] = "Content-Type: application/json";
$headers[] = "Content-Length: 1000";
$headers[] = "Authorization: Bearer MyAPIkey";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
正如傻瓜在 OP 下的评论中所述,问题在于硬编码的内容长度。为了更正,我构建了一个 POST 数据数组,json 对其进行编码并使用字符串长度传递适当的值。
$post_data = array(
'name' => '',
'force_ssl' => true,
'repo' => '',
);
$post_data = json_encode($post_data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);