我有一个POST到Mixcloud的PHP脚本,并收到我无法解决的错误。
这是使用PHP 7.3.6和curl的,就像我在其他文章中看到的那样。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.mixcloud.com/upload/?access_token=$mixcloud");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60 * 10);
curl_setopt($ch, CURLOPT_NOPROGRESS, true);
$server_output = curl_exec($ch);
curl_close ($ch);
// load json response into array
$res = json_decode($server_output, true);
echo print_r( $post, true );
echo print_r($res, true);
上传失败,并发送了以下帖子:
Array
(
[mp3] => @/var/share_ro/fullshow/surface.mp3
[name] => Upload title
[description] => Upload description
[picture] => @/var/share_ro/fullshow/avatar.png
[tags-0-tag] => Tag1
[tags-1-tag] => Tag2
[tags-2-tag] => Tag3
[tags-3-tag] => Tag4
[tags-4-tag] => Tag5
)
和响应:
Array
(
[details] => Array
(
[mp3] => Array
(
[0] => This field is required.
)
)
[error] => Array
(
[message] =>
[type] => PostValidationError
)
)
但是,该文件的完整路径确实存在,并且我尝试过在文件名上使用或不使用前导@
:
$ ls -lah /var/share_ro/fullshow/surface.mp3
-rw-r--r-- 1 user mp3 79M Jun 11 16:08 /var/share_ro/fullshow/surface.mp3
有人可以看到我在做什么错吗?
答案 0 :(得分:0)
我最终在shell脚本中使用curl,效果很好:
$script = "/home/user/code/scripts/mixcloud.sh";
$server_output = shell_exec("$script $mixcloud $mp3 '$name' '$host' '$title' '$desc' '$picfile' $tag1 $tag2");
脚本是:
#!/bin/bash
# Script to upload file to Mixcloud - see send_to_mixcloud function in make_fullshow.php
TOKEN=$1
MP3=$2
NAME=$3
ARTIST=$4
ALBUM=$5
DESC=$6
PIC=$7
TAG1=$8
TAG2=$9
TAG3=${10}
TAG4=${11}
TAG5=${12}
curl -F "mp3=@$MP3" \
-F "name=$NAME" \
-F "picture=@$PIC" \
-F "description=$DESC" \
-F "tags-0-tag=$TAG1" \
-F "tags-1-tag=$TAG2" \
-F "tags-2-tag=$TAG3" \
-F "tags-3-tag=$TAG4" \
-F "tags-4-tag=$TAG5" \
https://api.mixcloud.com//upload/?access_token=$TOKEN
答案 1 :(得分:0)
我返回尝试使用几年前发现的this script,但由于出现了同样的错误消息,它不再对我有用。您的回答对我很有帮助,但是我认为我应该将此“简化”发布到对我有用的工作上。完全一样,只是不需要外部shell脚本文件。
$script = 'curl';
foreach ($post as $key => $value) {
$script .= " -F \"$key=$value\"";
}
$script .= " https://api.mixcloud.com/upload/?access_token={$token}";
$server_output = shell_exec($script);