Amy
Amy

Reputation: 133

How to download a github json

I have a problem, I would try to download all the json generated by this url

https://api.github.com/repos/xxxx/xxxx/contents/xxxxxxx?ref=master

I have a json result like that

[
  {
    "name": "xxxx.png",
    "path": "xxxx/xxxx.png",
    "sha": "8b33da362caab310626daa2b70b232a98b38c6db",
    "size": 136356,
    "url": "https://api.github.com/repos/xxxx/xxxx/contents/xxxx/xxxx.png?ref=master",
    "html_url": "https://github.com/xxxx/xxxx/blob/master/xxxx/xxxx.png",
    "git_url": "https://api.github.com/repos/xxxx/xxxx/git/blobs/8b33da362caab310626daa2b70b232a98b38c6db",
    "download_url": "https://raw.githubusercontent.com/xxxx/xxxx/master/ModuleInfosJson/xxxx.png",
    "type": "file",
    "_links": {
      "self": "https://api.github.com/repos/xxxx/xxxx/contents/xxxx/xxxx.png?ref=master",
      "git": "https://api.github.com/repos/xxxx/xxxx/git/blobs/8b33da362caab310626daa2b70b232a98b38c6db",
      "html": "https://github.com/xxxx/module_apxxxx/blob/master/xxxx/xxxx.png"
    }
  }
]

I write this but it does'nt work and I have this message : failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden

Warning:    fopen([{"name":"xxxxxx.png","path":"xxxxxx/xxxxxxx.png","sha":"..............

My directory is in 777.

 $json = @file_get_contents($this->GetGithubRepo() . '/' . $module_name . '/contents/'  . $this->ModuleInfosJson  . '?ref=master', true, $this->context );
    file_put_contents(OSCOM::getConfig('dir_root', 'Shop') . $this->ModuleInfosJson . '/Cache/' . $module_name . '.json', fopen($json, 'r'));

but if I write the code like that, it's ok but I need the information like sha, git ..., That's I need, not in this case.

  $download = 'https://raw.githubusercontent.com/ClicShoppingAddsOn/' . $module_name . '/master/' . $this->ModuleInfosJson . '/' . $module_name . '.json';
    file_put_contents(OSCOM::getConfig('dir_root', 'Shop') . $this->ModuleInfosJson . '/Cache/' . $module_name . '.json', fopen($download, 'r'));

Thank you.

Upvotes: 0

Views: 4783

Answers (1)

Amy
Amy

Reputation: 133

I found a solution

   $local_file = OSCOM::getConfig('dir_root', 'Shop') . $this->ModuleInfosJson . '/Cache/' . $module_name . '.json';
        $remote_file = $this->GetGithubRepo() . '/' . $module_name . '/contents/'  . $this->ModuleInfosJson  . '?ref=master';

        $ch = curl_init();
        $fp = fopen ($local_file, 'w+');
        $ch = curl_init($remote_file);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
        curl_setopt($ch, CURLOPT_TIMEOUT, 50);
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_ENCODING, "");
        curl_exec($ch);
        curl_close($ch);
        fclose($fp);

Upvotes: 1

Related Questions