douren7921 2017-04-12 22:54
浏览 102
已采纳

如何在github,php中获取特定文件

I would know if it's possible to download a specific files (json) from github to insert a directory without to download all the files via a zip.

I have this

   $json = @file_get_contents($this->GetGithubRepo() . '/' . $module_name . '/contents/' . $this->ModuleInfosJson . '?ref=master', true, $this->context );

This line read the json, I would to write the json on a directory. Objective is to create a cache and read the cache before to read on github.

Thank you

  • 写回答

1条回答 默认 最新

  • dongzhan5286 2017-04-13 00:07
    关注

    Github is not "in love" with this behavior, but I have an entire framework that runs on the same paradigm. I do, however, use the zip. You can hit the raw content following this pattern:

    https://raw.githubusercontent.com/YOURHANDLE/THE_REPO/THE_BRANCH/FILE/PATH/ETC
    

    Look for the "raw" option on a particular file when browsing.

    Here is a config file from one of my repo in a format similar to how you wish:

    https://raw.githubusercontent.com/datamafia/ShopifyETL/master/config.cfg

    should currently return

    [all]
    SHOPIFY_KEY=YOUR-SHOPIFY-API-KEY
    SHOPIFY_PASSWORD=YOUR-SHOPIFY-API-PW
    SHOPIFY_STORE=YOUR-SHOPIFY-STORE-DOES-NOT-EXIST
    SHOPIFY_BASE_URL=SHOPIFY_STORE.myshopify.com-or-custom-FQDN
    

    Pay attention to document type and encoding, these could trip you up. Your JSON may not be JSON via the header (should not be).

    One final problem, beyond encoding, is that of private accounts. Once private a big can of work will be put on your plate to auth in and see the data.

    The header, mildly changed:

    Date: Thu, 13 Apr 2017 00:02:48 GMT
    Via: 1.1 varnish
    Cache-Control: max-age=300
    Etag: "154ec087bc75e501a18e72d4e14a6f17bc2f706b"
    Connection: keep-alive
    X-Served-By: cache-dfw1840-DFW
    X-Cache: HIT
    x-cache-hits: 1
    X-Timer: S1492012345.3876515,VS0,VE0
    Vary: Authorization,Accept-Encoding
    Access-Control-Allow-Origin: *
    X-Fastly-Request-ID: XYZABCXYZABCXYZABCXYZABCXYZABC
    Expires: Thu, 13 Apr 2017 00:07:48 GMT
    Source-Age: 35
    

    Document type is "plain" (text), so some casting and checking will be important. There are tools in PHP to handle the incoming data and use as JSON. Good luck.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?