【问题标题】:Upload asset to GitHub with PHP使用 PHP 将资产上传到 GitHub
【发布时间】:2014-04-26 18:05:33
【问题描述】:

我正在尝试编写一个 phing 构建任务,该任务将在此处将资产上传到 github。不幸的是,这意味着我需要将其写入 PHP 文件而不是 CLI(这是 GitHub http://developer.github.com/v3/repos/releases/#upload-a-release-asset 的发布 API)。实际上,这是在此处构建 CLI 查询,但使用 PHP Releasing a build artifact on 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 error curl_error() 函数的日志。

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

【问题讨论】:

    标签: php curl github


    【解决方案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',
    

    【讨论】:

    • 是的,似乎工作正常。我现在遇到了一堆 SSL 证书问题,但至少它正在连接到服务器!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 2018-05-15
    相关资源
    最近更新 更多