我正在尝试为我的公司制作一个程序,该程序接收图像并将它们发送到带有相关信息的API。
脚本获取标题,在我的系统上创建目录,将图像放在目录中,遍历目录,将每个图像转换为Base64Data,并将数据传递给将数据发布到API的python脚本。但由于某种原因,我的循环停在第一张图像上。
更新的代码:我试图使代码更容易理解。我从API获取了无效的base64代码。功能"照片"显示了XML必须构造的格式。
UPDATED - FIX:在PHP中使用cURL库,而不是将数据传递给python脚本。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$count = 0;
function photo($base64, $caption, $description, $displayname, $credits){
$data = "<image xmlns='http://apiapi.com/cms' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><caption>'";
$data .= $caption;
$data .= "'</caption>";
$data .= "<credits>'";
$data .= $credits;
$data .= "'</credits><data>'";
$data .= $base64;
$data .= "'</data>";
$data .="<description>'";
$data .= $base64;
$data .= "'</description><displayname>'";
$data .= $displayname;
$data .="'</displayname><extension>JPG</extension>";
$data .= "<imageusage>SlideShow</imageusage><uploadmethod>Server</uploadmethod>";
$data .= "</image>";
return $data; }
function base64url_encode($s) {
return str_replace(array('+', '/'), array('-', '_'), base64_encode($s));
}
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$dirty_title = $_POST['title'];
$title = htmlspecialchars($dirty_title);
chdir('uploads');
mkdir($title, 0777, true);
chdir($title);
//echo getcwd();
foreach ($_FILES['files']['name'] as $i => $name) {
$type = pathinfo($name, PATHINFO_EXTENSION);
$superstring = $title . $count .".". $type;
//echo "SuperString = " . $superstring."<br />";
if (strlen($_FILES['files']['name'][$i]) > 1) {
if (move_uploaded_file($_FILES['files']['tmp_name'][$i], $superstring)) {
$count++;
}
}
}
}
function endsWith($haystack, $needle)
{
return $needle === '' || substr_compare($haystack, $needle, -strlen($needle)) === 0;
}
if (isset($_POST['submit'])){
$curdir = getcwd();
//$exiftool = exec('exiftool -all= *.jpg');
//echo "exiftool = ".$exiftool."<br />";
$dir = new DirectoryIterator($curdir);
$count = 0;
foreach ($dir as $fileinfo){
if ($dir == '.' || $dir == '..' || endsWith($fileinfo, ".jpg_original")){}
else{
$count ++;
chmod($fileinfo, 0777);
$caption = $_POST['caption'];
$description = $_POST['description'];
$displayname = $_POST['displayname'];
$credit = $_POST['credits'];
$final_info = $curdir."/".$fileinfo;
$type = pathinfo($fileinfo, PATHINFO_EXTENSION);
$data = file_get_contents($final_info);
$mybase = base64url_encode($data);
echo "Function Worked";
$photo = photo($mybase, $caption, $description, $displayname, $credit);
$file_name = "data".$count.".txt";
$file = fopen($file_name, "w");
fwrite($file, $photo);
fclose($file);
$cwd = getcwd();
echo "REAL PATH: ".realpath($cwd);
}
}
$cwd = getcwd();
$realpath = realpath($cwd);
$test =exec("/usr/bin/python /var/www/html/helper_scripts/frankAPI.py '{$realpath}'");
echo $test;
}
?>
这是python脚本,它将文件路径作为参数,读取PHP脚本编写的写入数据,并将其发送到API。现在我得到500错误,它说我发送的base64编码不被识别为base64。
#!/usr/bin/python
import sys
import os
import requests
if __name__ == "__main__":
try:
directory = str(sys.argv[1])
headers = {'Authorization':'usernameusername',
'Content-Type': 'application/xml'
}
os.chdir(directory)
dirs = os.listdir('.')
for file in dirs:
if file.endswith(".txt"):
print file
with open(file, 'r') as content_file:
content = content_file.read()
r = requests.post('https://cms.worldnow.com/v1.0/images',
headers=headers,
data=content)
print r.text[0:100]
except Exception, e:
print e