【问题标题】:PHP cURL HTTP PUTPHP 卷曲 HTTP PUT
【发布时间】:2011-06-29 22:26:25
【问题描述】:

我正在尝试使用 cURL 创建一个 HTTP PUT 请求,但我无法使其工作。我读过很多教程,但没有一个真正奏效。这是我当前的代码:

$filedata = array('metadata' => $rdfxml);
$ch = curl_init($url);
$header = "Content-Type: multipart/form-data; boundary='123456f'";
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($filedata));
$returned = curl_exec($ch);

if (curl_error($ch))
{
    print curl_error($ch);
}
else
{
    print 'ret: ' .$returned;
}

我也尝试过使用 PHP PEAR,但得到了相同的结果。问题是存储库说没有设置元数据。我真的需要帮助!谢谢!

【问题讨论】:

    标签: php rest curl put http-put


    【解决方案1】:

    您混合了 2 个标准。

    错误在$header = "Content-Type: multipart/form-data; boundary='123456f'";

    函数http_build_query($filedata)仅适用于“Content-Type: application/x-www-form-urlencoded”,或无。

    【讨论】:

      【解决方案2】:

      在 POST 方法中,您可以放置​​一个数组。但是,在 PUT 方法中,您应该使用 http_build_query 来构建参数,如下所示:

      curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $postArr ) );
      

      【讨论】:

        【解决方案3】:

        使用 Postman for Chrome,选择 CODE 你会得到这个......并且可以工作

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

        【讨论】:

          【解决方案4】:

          今天我自己也这样做了...这是我为我工作的代码...

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

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

          【讨论】:

          • 请注意,我尝试使用curl_setopt($curl, CURLOPT_PUT, true); 以及此代码,但它不起作用,因此必须删除curl_setopt($curl, CURLOPT_PUT, true);
          • 如何读取 PUT 数据?我已经尝试了一切,但没有运气。 POST、GET 或 REQUEST 不起作用。
          • @andrebruton 我会试试file_get_contents('php://input')
          猜你喜欢
          • 2017-03-27
          • 2012-06-28
          • 1970-01-01
          • 1970-01-01
          • 2019-03-31
          • 2015-09-18
          • 2016-08-12
          • 2012-03-07
          相关资源
          最近更新 更多