我正在使用以下功能在远程服务器上发布数据和图像。
function do_post_request_put($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'PUT',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
我正在调用函数如下。
$arr[user][full_name]=$_POST[full_name];
$arr[user][email]=$_POST[email];
$arr[user][password]=$_POST[password];
$arr[user][profile_picture]=$_FILES[image];
$post_arr=http_build_query($arr);
$resp=do_post_request_put($url, $post_arr, $optional_headers = null);