PHP cURL HTTP PUT

后端 未结 4 765
一生所求
一生所求 2020-11-28 04:45

I am trying to create a HTTP PUT request with cURL and I can\'t make it work. I\'ve read many tutorials but none of them actually worked. Here\'s my current code:

         


        
相关标签:
4条回答
  • 2020-11-28 05:04

    Just been doing that myself today... here is code I have working for me...

    $data = array("a" => $a);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
    
    $response = curl_exec($ch);
    
    if (!$response) 
    {
        return false;
    }
    

    src: http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl

    0 讨论(0)
  • 2020-11-28 05:07

    Using Postman for Chrome, selecting CODE you get this... And works

    <?php
    
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://blablabla.com/comorl",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "PUT",
      CURLOPT_POSTFIELDS => "{\n  \"customer\" : \"con\",\n  \"customerID\" : \"5108\",\n  \"customerEmail\" : \"jordi@correo.es\",\n  \"Phone\" : \"34600000000\",\n  \"Active\" : false,\n  \"AudioWelcome\" : \"https://audio.com/welcome-defecto-es.mp3\"\n\n}",
      CURLOPT_HTTPHEADER => array(
        "cache-control: no-cache",
        "content-type: application/json",
        "x-api-key: whateveriyouneedinyourheader"
      ),
    ));
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    }
    
    ?>

    0 讨论(0)
  • 2020-11-28 05:07

    In a POST method, you can put an array. However, in a PUT method, you should use http_build_query to build the params like this:

    curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $postArr ) );
    
    0 讨论(0)
  • 2020-11-28 05:09

    You have mixed 2 standard.

    The error is in $header = "Content-Type: multipart/form-data; boundary='123456f'";

    The function http_build_query($filedata) is only for "Content-Type: application/x-www-form-urlencoded", or none.

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