亚马逊⼴告接⼝amazonadvertapi申请流程#亚马逊⼴告接⼝ amazon advert api 申请流程
⾄此 申请流程结束
然后会有 亚马逊的⼈员和你做邮件来往 询问你⼀些问题 类似于 API⽤途等 不啦不啦问⼀圈 然后会给你授权授权邮件的⼤概样式
后⾯还有⼀个要注意的地⽅
/v2/reports/{reportId}/download
会有个307重定向
curl
$realUrl = curl_getinfo($connection,CURLINFO_EFFECTIVE_URL);
获取重定向链接 重新下载 下载的⽂件是.gz 需要⾃⼰解压
// 解压gz⽂件的⽅法
// $gz_file  gz⽂件路径
function unzip_gz($gz_file)
{
echoStr("解压开始<br>");
echoStr("⽂件链接 ".$gz_file."<br>");
if(!file_exists($gz_file))
return false;
$buffer_size = 4096; // read 4kb at a time
$out_file_name = str_replace('.gz', '', $gz_file);
$file = gzopen($gz_file, 'rb');
$out_file = fopen($out_file_name, 'wb');
$str='';
while(!gzeof($file)) {
fwrite($out_file, gzread($file, $buffer_size));
}
fclose($out_file);
gzclose($file);
echoStr("解压完成<br>");
return $out_file_name;
}
获取过 reportId 后的下载⽂件并获取数据操作
个别参数需要⾃⼰修改下哈
/**
* 获取报告的内容
* reutrn json
*/
function getReportData($reportId)
{
echoStr("获取getReportData 中'".$reportId."'。。。<br>");
if(!$reportId)
{
echoStr($reportId." 获取失败<br>");
echoStr($reportId." 获取失败<br>");
return false;
}
// 判断⽂件状态 success才可以下载
$this->getReportStatus($reportId);
$reportUrl = $this->baseUrl."v2/reports/$reportId/download";
$realReportUrl = $this->jsonCurlAmazon($reportUrl,array(),"GET");
$filePath = $this->reportDownload($realReportUrl);
if(!$filePath)
return false;
$contents = $this->readTxt($filePath);
$this->filePath = $filePath;
return  $contents;
}
/**
* 下载⽂件并返回解压后的⽂件路径
*/
function reportDownload($url)
{
// 下载
$newFileGzList = $this->download_file($url,"amazon_advert_file");
$newFileGzPath = $newFileGzList["save_path"];
$newFileGzPathSize = $newFileGzList["file_size"];
if($newFileGzPathSize == 0)
{
// 删除⽂件
$this->del_file($newFileGzPath["file_name"]);
return false;
}
// 解压
$newFileName = $this->unzip_gz($newFileGzPath);
return $newFileName;
}
/**
* 判断⽂件状态
*/
function getReportStatus($reportId)
{
$reportUrl = $this->baseUrl."v2/reports/$reportId";
$reportInfo = $this->jsonCurlAmazon($reportUrl,array(),"GET");
$reportList = json_decode($reportInfo,true);
$status = $reportList["status"];
if($status == "SUCCESS")
return true;
else
{
echoStr("sleep 5<br>");
echoStr($reportInfo."<br>");
sleep(5);
$this->getReportStatus($reportId);
}
return true;
}
function jsonCurlAmazon($url,$info=array(),$method="POST")
{
$headList = array("Content-Type:application/json;","Authorization:".$this->accessToken,"Amazon-Advertising-API-ClientId:".$this->clientId);  if($this->apiScope)
$headList[] = "Amazon-Advertising-API-Scope:".$this->apiScope;
//初始化
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, $url);
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($connection, CURLOPT_HTTPHEADER, $headList);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, json_encode($info,true));
curl_setopt($connection, CURLOPT_CUSTOMREQUEST, $method); //设置请求⽅式  curl_setopt($connection, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($connection);
$realUrl = curl_getinfo($connection,CURLINFO_EFFECTIVE_URL);
//echoStr("<span style='color:red'>现实的访问url 为 </span>".$url."<br>");
//echoStr("<span style='color:blue'>真实的访问url 为 </span>".$realUrl."<br>");
curl_close($connection);
// 判断307
if($realUrl != $url)
{
return $realUrl;
$response = $this->jsonCurlAmazon($realUrl);
}
return $response;
}
/**
* 下载⽂件
*/
function download_file($url, $save_dir = './')
{
echoStr("下载⽂件中。。。<br>");
echoStr("⽂件链接:".$url."<br>");
if (trim($url) == '') {
return false;
}
$filename = date("Y-m-d-H-i-s")."_".$this->store.".";
//创建保存⽬录
if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true))
{
echo "⽬录失败";
return false;
}
$newFilePath = $save_dir."/". $filename;
ob_start();
readfile($url);
$content = ob_get_contents();
ob_end_clean();
//echo $content;
$size = strlen($content);
//⽂件⼤⼩
$fp2 = @fopen($newFilePath, 'a');
fwrite($fp2, $content);
fclose($fp2);
unset($content, $url);
$return  = array(
'file_name' => $filename,
'save_path' => $newFilePath,
'file_size' => $size
);
dump($return);
echoStr("下载完成<br>");
return $return ;
}
/**getsavefilename
* 解压GZ⽂件
*/
function unzip_gz($gz_file)
{
{
echoStr("解压开始<br>");
echoStr("⽂件链接 ".$gz_file."<br>");
if(!file_exists($gz_file))
return false;
$buffer_size = 4096; // read 4kb at a time
$out_file_name = str_replace('.gz', '', $gz_file);  $file = gzopen($gz_file, 'rb');
$out_file = fopen($out_file_name, 'wb');
$str='';
while(!gzeof($file)) {
fwrite($out_file, gzread($file, $buffer_size));  }
fclose($out_file);
gzclose($file);
echoStr("解压完成<br>");
return $out_file_name;
}
/**
* 删除⽂件
*/
function del_file($file_url)
{
//return true;
$res = false;
if(file_exists($file_url))
{
$res = unlink($file_url);
echoStr("⽂件删除完成<br>");
}
else
echoStr("⽂件不存在");
return $res;
}
// 获取每⾏的数据
function readTxt($file_path)
{
$handle = fopen($file_path, 'rb');
while (feof($handle)===false)
{
yield fgets($handle);
}
fclose($handle);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。