使用PHP中的Bings Ads下载广告系列效果报告

时间:2014-06-24 12:58:52

标签: php download fopen ads bing-api

自从一周以来,我就被震惊了。请告诉我是否有人可以帮助我解决这个问题。

我试过this samples他们给了。我正在尝试仅下载广告系列效果报告,我可以下载其中包含csv文件的zip文件。 Here是我为关键字所遵循的另一个直接示例,并为广告系列效果采取了相同的方式。这给了我一个下载报告的链接。当我试图手动下载网址时,我可以下载,但我无法通过我的代码下载。

function DownloadFile($reportDownloadUrl, $downloadPath) {
    if (!$reader = fopen($reportDownloadUrl, 'rb')) {
        throw new Exception("Failed to open URL " . $reportDownloadUrl . ".");
    }
    if (!$writer = fopen($downloadPath, 'wb')){
        fclose($reader);
        throw new Exception("Failed to create ZIP file " . $downloadPath . ".");
    }
    $bufferSize = 100 * 1024;
    while (!feof($reader)) {
        if (false === ($buffer = fread($reader, $bufferSize))) {
            fclose($reader);
            fclose($writer);
            throw new Exception("Read operation from URL failed.");
        }

        if (fwrite($writer, $buffer) === false) {
            fclose($reader);
            fclose($writer);
            $exception = new Exception("Write operation to ZIP file failed.");
        }
    }
    fclose($reader);
    fflush($writer);
    fclose($writer);
}

但我无法下载该文件。我不能从那里前进,所以任何帮助,如以任何其他形式下载报告或当前方法中的简单代码更改,非常感谢。提前致谢。

1 个答案:

答案 0 :(得分:0)

当我尝试这个时,我也遇到了DownloadFile函数的问题,所以用不同的版本替换它

function DownloadFile($reportDownloadUrl, $downloadPath) {
    $url    = $reportDownloadUrl;
    // Example for the path would be in this format
    // $path = '/xxx/yyy/reports/keywordperf.zip';
    // using the server path and not relative to the file
    $path   = $downloadPath;

    $fp     = fopen($path, 'w');
    $ch     = curl_init($url);

    curl_setopt($ch, CURLOPT_SSLVERSION, 3);
    curl_setopt($ch, CURLOPT_FILE, $fp);

    if($result = curl_exec($ch)) {
        $status = true;
    }
    else
    {
        $status = false;
    }

    curl_close($ch);
    fclose($fp);

    return status;
}