2017-04-14 157 views
0

我有一個問題,我會嘗試通過這個網址下載所產生的所有的JSON如何下載GitHub的JSON

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

我有一個JSON結果一樣,

[ 
    { 
    "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" 
    } 
    } 
] 

我寫這篇文章但它沒有工作,我有這個消息:未能打開流:HTTP請求失敗! HTTP/1.0 403禁止

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

我的目錄是在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')); 

,但如果我寫這樣的代碼,這是確定,但我需要一個像啥信息,混帳......,這就是我需要,而不是在這種情況下。

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

謝謝。

回答

1

我找到了解決辦法

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