我必须做一个PHP脚本,这将自动上传数据到使用API的谷歌分析。但是有一个问题:开发者指南只针对python和java编写。针对PHP的Google api客户端库已经有了上传方法。请帮我翻译一下这段java代码:
File file = new File("data.csv");
InputStreamContent mediaContent = new InputStreamContent('application/octet-stream', new FileInputStream(file));
mediaContent.setLength(file.length());
Upload upload = analytics.management().dailyUploads().upload("1234",
"UA-1234-1", "123456789", "2012-10-31", 1, "cost", mediaContent);
upload.setReset(true);
DailyUploadAppend append = upload.execute();
或者这个python代码:
media = MediaFileUpload('data.csv', mimetype='application/octet-stream', resumable=False)
daily_upload = analytics.management().dailyUploads().upload(
accountId='1234',
webPropertyId='UA-1234-1',
customDataSourceId='123456789',
date='2012-10-31',
appendNumber=1,
reset=true,
type='cost',
media_body=media).execute()
你让这个工作了吗?你试过什么?我不是专家,但我会试一试的。
他们网站上的PHP库绝对支持这个,在Google api客户端库中列出了"dailyUploads"的方法。
$dailyUploadsFile = $service->management_dailyUploads->upload(
'12345678', // your accountID
'UA-12345678-1', // your web property ID
'xXxXxXxXxXxXxXxXxXxXxX', // your custom data source UID
'2013-02-04', // date
1, // append number
'cost', // type of data
array(
"reset" => true,
"data" => file_get_contents("your-data.csv"),
'mimeType' => 'application/octet-stream',
'uploadType' => 'media'));
这一切都来自类"Google_ManagementDailyUploadsServiceResource"。
连接需要进行身份验证。如果您的输入文件包含多天的数据,则可以包含结束日期。