1

因此,我现在可以使用 curl 命令来获取有关我在 netlify 上的站点的信息。但是,根据 API 文档,我应该也可以使用 POST 创建站点。对于我的生活,我似乎无法弄清楚发生了什么。运行此页面时,页面思考了很长时间,然后刷新并返回一个空响应,并且没有创建站点。

                    $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 文档,特别是指“创建站点”部分(第 3 下)https://www.netlify.com/docs/api/#sites

知道我在这里缺少什么吗?就像我说的那样,我正确地进行了身份验证,并且可以从我的帐户中获取我想要的所有数据。我似乎无法发布任何内容。


************************************************* 更新*************************************************


我现在传递了一些属性,但是得到了一个请求超时。

                    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);
4

1 回答 1

1

正如傻瓜在 OP 下的评论中所说,问题是硬编码的 Content-Length。为了更正,我构建了一个 POST 数据数组,对其进行了 json 编码并使用字符串长度来传递适当的值。

                    $post_data = array(
                        'name'      => '',
                        'force_ssl' => true,
                        'repo'      => '',
                    );
                    $post_data = json_encode($post_data);

                    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
于 2018-06-05T20:11:08.417 回答