带有数据的PHP CURL PUT请求

时间:2022-10-19 18:23:34

I need to do a PUT request sending json data, I wrote the code below but the data are not sent. Can someone help?

我需要发送一个PUT请求发送json数据,我写了下面的代码,但数据没有发送。有人可以帮忙吗?

$url = 'https://example.com';
$data='{"example":"valor"}';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);     
print_r($result);

thanks

谢谢

4 个解决方案

#1


1  

I think I know your issue. Try adding

我想我知道你的问题。尝试添加

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ curl,CURLOPT_RETURNTRANSFER,true);

The PHP docs allude that if you don't do this, executing the curl will just output the contents directly to a page, which if you're using this as a script that doesn't output anything, will obviously not show anything. This should make $result equal something, and then allow you to print_r it

PHP文档提到,如果你不这样做,执行curl只会将内容直接输出到页面,如果你使用它作为不输出任何内容的脚本,显然不会显示任何内容。这应该使$ result等于某个东西,然后允许你print_r它

Reference: http://php.net/manual/en/function.curl-setopt.php

参考:http://php.net/manual/en/function.curl-setopt.php

#2


0  

I would suggest you use Guzzle. It is a wrapper around CURL that abstracts away all the configuration and gives you many convenience methods for easy use.

我建议你使用Guzzle。它是CURL的一个包装器,它抽象出所有配置,并为您提供了许多便于使用的便捷方法。

You can install Guzzle by following instructions in the following link: http://guzzle.readthedocs.org/en/latest/overview.html#installation

您可以按照以下链接中的说明安装Guzzle:http://guzzle.readthedocs.org/en/latest/overview.html#installation

Below is an example of how you could use guzzle to get what you want.

下面是一个如何使用guzzle来获得你想要的东西的例子。

    require 'vendor/autoload.php';    
    use GuzzleHttp\Client;

    $url = 'https://example.com';
    $data= array("example" => "valor");

    $client = new Client();
    $response = $client->request('PUT', $url, [
        'json' => $data
    ]);

    print_r($response);

I have used Guzzle in place of raw CURL on many projects and found it to be a great time saver.

在许多项目中我使用Guzzle代替原始CURL,并发现它可以节省大量时间。

#3


0  

For PUT use curl_setopt($ch, CURLOPT_PUT, true);

对于PUT,使用curl_setopt($ ch,CURLOPT_PUT,true);

Refer this: How to start a GET/POST/PUT/DELETE request and judge request type in PHP?

请参阅:如何在PHP中启动GET / POST / PUT / DELETE请求并判断请求类型?

#4


0  

I had the same problem. I've solved by changing

我有同样的问题。我已经通过改变解决了

curl_setopt($curl, CURLOPT_URL, $url);

to

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");

Before, this, curl try to post a file or something similar, with 100-continue, but we need to post json.

之前,这个,curl尝试发布一个文件或类似的东西,100-continue,但我们需要发布json。

#1


1  

I think I know your issue. Try adding

我想我知道你的问题。尝试添加

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ curl,CURLOPT_RETURNTRANSFER,true);

The PHP docs allude that if you don't do this, executing the curl will just output the contents directly to a page, which if you're using this as a script that doesn't output anything, will obviously not show anything. This should make $result equal something, and then allow you to print_r it

PHP文档提到,如果你不这样做,执行curl只会将内容直接输出到页面,如果你使用它作为不输出任何内容的脚本,显然不会显示任何内容。这应该使$ result等于某个东西,然后允许你print_r它

Reference: http://php.net/manual/en/function.curl-setopt.php

参考:http://php.net/manual/en/function.curl-setopt.php

#2


0  

I would suggest you use Guzzle. It is a wrapper around CURL that abstracts away all the configuration and gives you many convenience methods for easy use.

我建议你使用Guzzle。它是CURL的一个包装器,它抽象出所有配置,并为您提供了许多便于使用的便捷方法。

You can install Guzzle by following instructions in the following link: http://guzzle.readthedocs.org/en/latest/overview.html#installation

您可以按照以下链接中的说明安装Guzzle:http://guzzle.readthedocs.org/en/latest/overview.html#installation

Below is an example of how you could use guzzle to get what you want.

下面是一个如何使用guzzle来获得你想要的东西的例子。

    require 'vendor/autoload.php';    
    use GuzzleHttp\Client;

    $url = 'https://example.com';
    $data= array("example" => "valor");

    $client = new Client();
    $response = $client->request('PUT', $url, [
        'json' => $data
    ]);

    print_r($response);

I have used Guzzle in place of raw CURL on many projects and found it to be a great time saver.

在许多项目中我使用Guzzle代替原始CURL,并发现它可以节省大量时间。

#3


0  

For PUT use curl_setopt($ch, CURLOPT_PUT, true);

对于PUT,使用curl_setopt($ ch,CURLOPT_PUT,true);

Refer this: How to start a GET/POST/PUT/DELETE request and judge request type in PHP?

请参阅:如何在PHP中启动GET / POST / PUT / DELETE请求并判断请求类型?

#4


0  

I had the same problem. I've solved by changing

我有同样的问题。我已经通过改变解决了

curl_setopt($curl, CURLOPT_URL, $url);

to

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");

Before, this, curl try to post a file or something similar, with 100-continue, but we need to post json.

之前,这个,curl尝试发布一个文件或类似的东西,100-continue,但我们需要发布json。