Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PHP下载Github发布资产文件_Php_Api_Github_Curl_Download - Fatal编程技术网

通过PHP下载Github发布资产文件

通过PHP下载Github发布资产文件,php,api,github,curl,download,Php,Api,Github,Curl,Download,我正在使用PHP Github API列出我的版本。现在,我需要将最新版本的资产文件复制到我的服务器 PHP Github API不提供下载功能,因此我决定直接发出一个cURL请求 这是我的代码: <pre> <?php // This file is generated by Composer require_once '../vendor/autoload.php'; $client = new \Github\Client(); $client->authent

我正在使用PHP Github API列出我的版本。现在,我需要将最新版本的资产文件复制到我的服务器

PHP Github API不提供下载功能,因此我决定直接发出一个cURL请求

这是我的代码:

<pre>
<?php

// This file is generated by Composer
require_once '../vendor/autoload.php';

$client = new \Github\Client();
$client->authenticate(':mytoken', null, Github\Client::AUTH_ACCESS_TOKEN);
$release = $client->api('repo')->releases()->latest('arminetsw', 'webstore');
$nombre_fichero = $release['assets'][0]['name'];
$download_url = $release['assets'][0]['browser_download_url'];
$download_url = 'https://api.github.com/repos/arminetsw/webstore/releases/assets/:myAssetId?access_token=:mytoken';

$cliente = curl_init();
$file = fopen("webstore.zip", 'w');
curl_setopt($cliente, CURLOPT_URL, "https://api.github.com/repos/arminetsw/webstore/releases/assets/32188729?access_token=:mytoken");
curl_setopt($cliente, CURLOPT_HEADER, 'Accept: application/octet-stream');
curl_setopt($cliente, CURLOPT_USERAGENT, 'Webstore');
curl_exec($cliente);
curl_close($cliente);
fclose($file);

//$nuevo_fichero = ''

/*if (!copy($download_url, $nombre_fichero)) {
    echo "Error al copiar $nombre_fichero...\n";
}*/

var_dump($release);

?>
</pre>
我只得到一个0字节的webstore.zip文件,没有错误

*回购是私有的。

最终工作代码:

<pre>
<?php

set_time_limit(0);

require_once '../vendor/autoload.php';

$client = new \Github\Client();
$client->authenticate('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', null, Github\Client::AUTH_ACCESS_TOKEN);
$release = $client->api('repo')->releases()->latest('user', 'repo');
$nombre_fichero = $release['assets'][0]['name'];
$download_url = $release['assets'][0]['url'];
$authorization = "access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$download_url .= "?" . $authorization;

$file = fopen($nombre_fichero, 'w');
$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => $download_url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_USERAGENT => "Webstore",
  CURLOPT_FILE => $file,
  CURLOPT_HTTPHEADER => [
    "Accept: application/octet-stream",
    "Content-Type: application/octet-stream"
  ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

fputs($file, $response);
fclose($file);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

?>
</pre>