CURL;将post发送为数组(CURLOPT_POSTFIELDS)或字符串。我正在与gamecrafter的API接口,而CURLOPT_POSTFIELDS对于数组来说表现得很奇怪。我在API中使用了API(请参阅index.html),但现在我需要隐藏用户名/密码,以便我切换到PHP。除了发送图像文件之外,新界面工作(文件是用javascript生成的,现在发送到我的php页面而不是gamecrafter,但使用相同的代码)。该图像使其成为php文件,但是我尝试了许多排列将其发送到API但没有成功(请参阅下面的结果)。任何帮助都将非常感激。
---设置---
godaddy服务器 PHP版本:5.4.34
--- gamecrafter.php ---
...
$filename = $_FILES['file']['tmp_name'];
$handle = fopen($filename, "r");
$data = array(
'session_id' => $sessionID,
'folder_id' => $rootFolderID,
'name' => 'image.png',
1) // 'file' => '@' . $filename . ';filename=name.txt' . ';type=image/png',
);
2) //$data = http_build_query($data);
$ch = curl_init();
3) //curl_setopt($ch, CURLOPT_INFILE, $handle);
3) //curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filename));
3) //curl_setopt($ch, CURLOPT_PUT, true);
4) //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt ($ch, CURLOUT_IGNORE_CONTENT_LENGTH, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
curl_setopt($ch, CURLOPT_URL, 'https://www.thegamecrafter.com/api/file');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, 1);
...
--- index.html ---
...
<javascript>
...
var data = new FormData();
var imgBin = window.atob(canvas.toDataURL("image/png").split(',')[1]);
var array = [];
for (var i = 0; i < imgBin.length; i++) {
array.push(imgBin.charCodeAt(i));
}
data.append("file", new Blob([new Uint8Array(array)], {type: 'image/png'}));
data.append("name", "image.png");
data.append("folder_id", user.root_folder_id);
data.append("session_id", session.id);
var request = new XMLHttpRequest();
//request.open("POST", "https://www.thegamecrafter.com/api/file", false);
request.open("POST", "gamecrafter.php", false);
request.onreadystatechange = function() {
if (request.readyState == 4) {
fileResult = request.responseText;
}
}
request.send(data);
...
---结果---
cases show what commenting or commenting labelled lines will result in. Thus ?tf? means line 1) can be commented or not, 2) is uncommented, 3) is commented and 4) can be either.
case ftff - No file uploaded. You must specify a file to upload
case ttff - 'file' is not a text field. You need to upload a file.
case ?fff - No file uploaded. You must specify a file to upload
case ??tf - Resource Not Found
case ???t - Internal Server Error