使用PHP将资产上传到GitHub

时间:2014-03-20 04:52:46

标签: php curl github

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

中构建CLI查询

所以我有一个通用的卷发功能,现在正在定制它的地狱

/** 
 * 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

这看起来像我正在阅读的关于卷曲功能和基于API的正确网址(请随意说这是不是真的!)但是我正在点击Failed connect to uploads.github.com:1; No error函数日志中的错误curl_error()

有没有人有任何想法/帮助他们可以给?如果您想了解更多信息,可以在https://github.com/JoomJunk/Accordion/blob/development/build/phingext/GituploadTask.php

找到完整的Phing任务

1 个答案:

答案 0 :(得分:1)

您的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',