dswqz24846 2014-11-27 01:54
浏览 154

PHP中的CURL; 将post发送为数组(CURLOPT_POSTFIELDS)或字符串

CURL; sending post as array (CURLOPT_POSTFIELDS) or string. I am interfacing with an API for gamecrafter and CURLOPT_POSTFIELDS is behaving strangely for arrays. I got the API working in javascript (see index.html) but now I need to hide the username/password so I am switching to PHP. The new interface works except for sending image files (files are generated in javascript and sent to my php page now instead of gamecrafter, but use the same code). The image makes it to the php file OK, but I have tried many permutations to send it on to the API with no success (see results below). Any help would be most appreciated.

--- setup ---

godaddy server PHP version: 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);

...

--- results ---

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
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 wegame打不开英雄联盟
    • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
    • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
    • ¥30 eclipse开启服务后,网页无法打开
    • ¥30 雷达辐射源信号参考模型
    • ¥15 html+css+js如何实现这样子的效果?
    • ¥15 STM32单片机自主设计
    • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
    • ¥15 不小心不正规的开发公司导致不给我们y码,
    • ¥15 我的代码无法在vc++中运行呀,错误很多