Convert to PHP REST CURL POST

2019-02-19 07:16发布

问题:

How can we convert this code to 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--

回答1:

$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);

Edit: Didn't read all of post, fixed.



回答2:

I hope it is helpful:

 <?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);

In this example I am sending a header, and post fields.

$headers => I put a token value in a array, just to show how can you send a value.

$data => I used a array with some imagine values.

In the curl I used this variebles here: curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

CURLOPT_POSTFIELDS -> the full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix.

If you would want to learn more about curl_setopt, I sugest you this link: http://php.net/manual/pt_BR/function.curl-setopt.php

To see what you send and to receive the file that you send, you can use this code bellow:

    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']));