我在将文件上传到Imageshack的API时遇到了一些麻烦。我使用multipart / form-data表单来获取文件。
的index.php:
<form method="post" enctype="multipart/form-data" action="upload.php">
<input type="file" name="fileupload"/>
<input type="submit" value="Go"/>
</form>
通常我对此没有任何问题,但是数据必须发送到http://imageshack.us/upload_api.php并且响应在他们的服务器上的XML样式的HTML页面中返回,所以我真的无法用它做什么。所以我决定通过PHP cURL脚本传递表单并在同一页面上获取响应。
upload.php的:
<?php
$url = 'http://imageshack.us/upload_api.php';
$key = '4BEILRTV5ff57ecb70867e8becb2c4b5e695bdb4';
$max_file_size = '5242880';
$temp = $_FILES["fileupload"]["tmp_name"];
$name = $_FILES["fileupload"]["name"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
"fileupload" => '@' . $temp,
"key" => $key,
"max_file_size" => $max_file_size
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
echo $response;
?>
起初我遇到了一堆错误,但现在我没有得到任何回复。甚至不是错误。
使用此方法的任何建议都会很棒!
答案 0 :(得分:1)
我必须包含“format”=&gt; 'json'进入post选项并使用json_decode来解析信息。这是upload.php脚本。
<?php
$url = 'http://imageshack.us/upload_api.php';
$key = '4BEILRTV5ff57ecb70867e8becb2c4b5e695bdb4';
$max_file_size = '5242880';
$temp = $_FILES["fileupload"]["tmp_name"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $url);
$post = array(
"fileupload" => '@' . $temp,
"key" => $key,
"format" => 'json',
"max_file_size" => $max_file_size,
"Content-type" => "multipart/form-data"
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
$json_a=json_decode($response,true);
echo $json_a[links][image_link];
?>