使用PHP将资产上传到GitHub

乔治·威尔逊

我正在尝试编写一个phing构建任务,该任务将在此处将资产上传到github。不幸的是,这意味着我需要用PHP文件而不是CLI编写它(这是GitHub的发布API http://developer.github.com/v3/repos/releases/#upload-a-release-asset)。实际上,这是在这里构建CLI查询,但是在PHP中在github上发布了构建工件

所以我有一个通用的curl post函数,现在正在自定义它

/** 
 * Send a POST request using cURL
 *
 * @param   UriInterface   $url      The url and request containing the post information
 * @param   array          $options  Extra options for cURL. This can also override the defaults
 *
 * @return  string The response of the object
 */
private function curl_post(UriInterface $url, array $options = array())
{
    $this->log('Attempting to upload file with URL ' . $url->toString(), Project::MSG_INFO);
    $defaults = array(
        CURLOPT_POST => 1,
        CURLOPT_HEADER => 1,
        CURLOPT_FRESH_CONNECT => 1,
        CURLOPT_FORBID_REUSE => 1,
        CURLOPT_TIMEOUT => 4,
        CURLOPT_POSTFIELDS => $url->getQuery(),
    );

    // Initiate CURL
    $ch = curl_init($url->toString());

    // Create the full params
    $params = array_merge($options, $defaults);
    curl_setopt_array($ch, $params);

    if(!$result = curl_exec($ch)) 
    {
        $this->log(curl_error($ch), Project::MSG_ERR);

        return curl_error($ch);
    }

    curl_close($ch);

    return $result;
}

就这篇文章而言,它与UriInterface无关紧要,我已经检查过它给出了正确的结果:)

然后,我将其称为:

        $pageUrl = "https://uploads.github.com/repos/" . $this->owner . '/' . $this->repo . "/releases/" . $this->version . "/assets?name=";

        $fullUrl = $pageUrl . $filename;

        $headers = array(
            'Content-Type: ' . $header,
            'Accept: application/vnd.github.manifold-preview',
            'Authorization: token TOKEN',
        );

        $options = array(
            CURLOPT_SSL_VERIFYPEER => false, // Despite SSL is 100% supported to suppress the Error 60 currently thrown
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_BINARYTRANSFER => 1 // --data-binary
        );

        // Create the Uri object
        $url = new Uri($fullUrl);

        $url->setQuery(array('file' => "@$filename"));
        $response = $this->curl_post($url, $options);

第一个日志输出 Attempting to upload file with URL https://uploads.github.com/repos/JoomJunk/Accordion/releases/3.0.2/assets?file=@mod_accordion-3.0.2.zip

从我阅读的有关curl函数的内容和基于API的内容来看,这看起来像是正确的URL(请随意说这是否不正确!),但是我Failed connect to uploads.github.com:1; No errorcurl_error()函数的日志中遇到了错误

有没有人可以提供任何想法/帮助?如果您想了解更多信息,可以在https://github.com/JoomJunk/Accordion/blob/development/build/phingext/GituploadTask.php中找到完整的Phing任务。

萨布吉·哈桑(Sabuj Hasan)

您的API文档说Send the raw binary content of the asset as the request body因此,您POSTFIELDS应该是:

CURLOPT_POSTFIELDS => file_get_contents("file.zip"),

您没有提到$header变量中的内容。它应该是application/zip

// 'Content-Type: ' . $header,
'Content-Type: application/zip',

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章