我们如何将此代码转换为PHP REST CURL POST?
POST https://apis.live.net/v5.0/me/skydrive/files?access_token=ACCESS_TOKEN
Content-Type: multipart/form-data; boundary=A300x
--A300x
Content-Disposition: form-data; name="file"; filename="HelloWorld.txt"
Content-Type: application/octet-stream
Hello, World!
--A300x--
$url = 'POST https://apis.live.net/v5.0/me/skydrive/files';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POSTFIELDS, array('access_token' => TOKEN, 'name' => 'file', 'filename' => "@HelloWorld.txt"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
编辑:没有阅读所有帖子,已修复。
我希望它对您有所帮助:
<?php
$headers = array('token: pacexpressx1x2x3j' );
$data = array(
"STATUS" => "insert" ,
"NAME" => utf8_encode("Test integration 10"),
"COD_AREA" => "4",
"DESC_AREA_" => utf8_encode("info/IT"),
"JOB" => utf8_encode("TEST job"),
"COMPANY" => "4",
"DESC_COMPANY" => utf8_encode("XPTO1"),
"file" => '@/home/user/test.png');
$url = 'your_link';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch);
echo($output) . PHP_EOL;
curl_close($ch);
在此示例中,我发送标头和发布字段。
$headers => 我将一个令牌值放在数组中,只是为了展示如何发送值。
$data => 我使用了一个带有一些虚数值的数组。
在卷曲中,我在这里使用了这个品种:curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string(;curl_setopt($ch、CURLOPT_HTTPHEADER、$headers(;
CURLOPT_POSTFIELDS ->要在 HTTP "POST" 操作中发布的完整数据。要发布文件,请在文件名前面加上 @ 并使用完整路径。可以通过在文件名后面加上格式为";type=mimetype"的类型来显式指定文件类型。此参数可以作为 urlencoding 字符串传递,例如 'para1=val1¶2=val2&...'或作为数组,字段名称作为键,字段数据作为值。如果值是数组,则内容类型标头将设置为多部分/表单数据。从 PHP 5.2.0 开始,如果文件以 @ 前缀传递给此选项,则该值必须是数组。
如果您想了解有关curl_setopt的更多信息,我建议您此链接:http://php.net/manual/pt_BR/function.curl-setopt.php
要查看您发送的内容和接收您发送的文件,您可以使用以下代码:
echo 'Here is some more debugging info:';
echo "'n<hr />'n";
print_r($_FILES);
echo "'n<hr />'n";
print_r($_POST);
print "</pr" . "e>'n";
echo "'n<hr />'n";
// here you will record the file in your server... you can choose a directory if you want
file_put_contents($_FILES['file']['name'],file_get_contents($_FILES['file']['tmp_name']));