0

I'm trying to write a phing build task that will upload a asset to github here. Unfortunately this means I need to write it in a PHP file rather than in CLI (This is the release API for GitHub http://developer.github.com/v3/repos/releases/#upload-a-release-asset). Effectively this is building the CLI query here but in PHP Releasing a build artifact on github

So I've got a generic curl post function and am now customizing the hell out of it

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

For the sake of this post it doesn't really matter about UriInterface I've checked it's giving the correct result :)

Then I call this with:

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

The first log outputs 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

Which looks like the correct URL from what I've read about the curl function and based on the API (please feel free to say if this isn't true!) however I'm hitting an error Failed connect to uploads.github.com:1; No error in the log of the curl_error() function.

Does anyone have any ideas/help they can give? If you want more information the full Phing task can be found at https://github.com/JoomJunk/Accordion/blob/development/build/phingext/GituploadTask.php

Community
  • 1
  • 1
George Wilson
  • 5,595
  • 5
  • 29
  • 42

1 Answers1

1

Your API doc says Send the raw binary content of the asset as the request body. So your POSTFIELDS should be:

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

You didn't mention what you have in your $header variable. It should be application/zip

// 'Content-Type: ' . $header,
'Content-Type: application/zip',
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • Yup that seems to have worked OK. I'm now hitting a crap load of SSL certificate problems but at least it's connecting to the server! Thanks! – George Wilson Mar 20 '14 at 14:21