至此 申请流程结束
然后会有 亚马逊的人员和你做邮件来往 询问你一些问题 类似于 API用途等 不啦不啦问一圈 然后会给你授权
授权邮件的大概样式
最后跳转到重定向链接
https://你的重定向链接?code=*******
这个就是需要的code
2 获取访问令牌和刷新令牌
code 有时效性 尽快回去访问令牌
3 获取配置文件
需要使用到 刷新令牌
v2/profiles
分 NA FE EU3个区域 不同区域授权的账号是各自区域的 不然获取不到指定区域的配置文件 而且 https://advertising-api.amazon.com/ 这个链接也分地区 后来问客服要得
https://advertising-api-fe.amazon.com/ //FE
https://advertising-api-eu.amazon.com/ //EU
https://advertising-api.amazon.com/ //NA
后面还有一个要注意的地方
/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>");
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_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.".json.gz";
//创建保存目录
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 ;
}
/**
* 解压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);
}