PHP cURL HTTP PUT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5043525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:37:35  来源:igfitidea点击:

PHP cURL HTTP PUT

phprestcurlputhttp-put

提问by user601513

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:

我正在尝试使用 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;
}

I've also tried using PHP PEAR but got the same result. The problem is that the repository says that no metadata has been set. I really need help! Thanks!

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

回答by Brian

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

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

回答by Jordi Serra

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

使用 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\" : \"[email protected]\",\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;
}

?>

回答by beck bi

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

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

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

回答by Fabian Maximiliano Lucena Gome

You have mixed 2 standard.

您混合了 2 个标准。

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

错误在 $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.

该功能http_build_query($filedata)仅适用于“内容类型:应用程序/x-www-form-urlencoded”,或无。