【问题标题】:Trouble using PHP curl to download github repo使用 PHP curl 下载 github repo 时遇到问题
【发布时间】:2018-12-20 04:42:22
【问题描述】:

根据标题,我正在尝试使用 php curl 库从 github 下载 repo。这是我的代码:

        $url = 'https://api.github.com/repos/SeanPeterson/Raspberry-Pi-Case/zipball/master';
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_USERAGENT, 'SeanPeterson');
        $content = curl_exec($curl);

        if(curl_errno($curl)){
            $this->respond('error:' . curl_error($curl));
         }

        curl_close($curl);

此代码的结果是一个损坏的 zip 文件,在尝试解压缩时会变成一个 cpgz 文件。这是使用文本编辑器打开 zip 文件时存在的标题信息

HTTP/1.1 302 Found
Server: GitHub.com
Date: Wed, 19 Dec 2018 18:47:52 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 0
Status: 302 Found
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 60
X-RateLimit-Reset: 1545248872
Cache-Control: public, must-revalidate, max-age=0
Expires: Wed, 19 Dec 2018 18:47:52 GMT
Location: https://codeload.github.com/SeanPeterson/Raspberry-Pi-Case/legacy.zip/master
Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type
Access-Control-Allow-Origin: *
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Frame-Options: deny
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
Content-Security-Policy: default-src 'none'
X-GitHub-Request-Id: D01E:1971:33F0754:7030B7C:5C1A9258

HTTP/1.1 200 OK
Transfer-Encoding: chunked
Access-Control-Allow-Origin: https://render.githubusercontent.com
Content-Security-Policy: default-src 'none'; style-src 'unsafe-inline'; sandbox
Strict-Transport-Security: max-age=31536000
Vary: Authorization,Accept-Encoding
X-Content-Type-Options: nosniff
X-Frame-Options: deny
X-XSS-Protection: 1; mode=block
ETag: "09b31138d130b657cea3c3b5e12191fa7f48c558"
Content-Type: application/zip
Content-Disposition: attachment; filename=SeanPeterson-Raspberry-Pi-Case-09b3113.zip
X-Geo-Block-List: 
Date: Wed, 19 Dec 2018 18:47:53 GMT
X-GitHub-Request-Id: D01F:77CB:DA101:20BE73:5C1A9258

当用浏览器点击链接时,它会完美下载文件。所以形成这个,我认为我在使用 curl 的方式上一定犯了一个错误。

非常感谢任何见解!

【问题讨论】:

标签: php github-api php-curl


【解决方案1】:

试试这个:

    $url = 'https://api.github.com/repos/SeanPeterson/Raspberry-Pi-Case/zipball/master';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_URL, $url);

    curl_setopt($curl, CURLOPT_USERAGENT, 'SeanPeterson');
    $content = curl_exec($curl);


    $err = curl_error($curl);

    curl_close($curl);

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

    $fp = fopen("test.zip","wb");
    fwrite($fp,$content);
    fclose($fp);

认为包含标题可能会损坏 zip。

【讨论】:

  • 是的,现在这似乎很明显。谢谢!
  • 很高兴我能帮上忙 :)
猜你喜欢
  • 2023-04-04
  • 1970-01-01
  • 2023-03-23
  • 2019-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多