Convert to PHP REST CURL POST

后端 未结 2 463
庸人自扰
庸人自扰 2021-01-14 10:59

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         


        
相关标签:
2条回答
  • 2021-01-14 11:19

    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 an array with some imaginary 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 the value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, the 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 suggest 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 below:

        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']));
    
    0 讨论(0)
  • 2021-01-14 11:20
    $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.

    0 讨论(0)
提交回复
热议问题