PHP cURL HTTP PUT
我正在尝试使用 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 但得到了相同的结果。问题是存储库表示尚未设置元数据。我真的需要帮助!谢谢!
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:
$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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
今天我自己就这么做了...这是我为我工作的代码...
src: http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl
Just been doing that myself today... here is code I have working for me...
src: http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl
使用 Chrome 的 Postman,选择 CODE 你会得到这个......并且有效
Using Postman for Chrome, selecting CODE you get this... And works
在 POST 方法中,您可以放置一个数组。但是,在 PUT 方法中,您应该使用
http_build_query
来构建如下参数: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:您混合了 2 个标准。
错误位于
$header = "Content-Type: multipart/form-data; border='123456f'";
函数
http_build_query($filedata)
仅适用于 " Content-Type:application/x-www-form-urlencoded”,或无。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.