2

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

$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

Luciano Marqueto
  • 1,148
  • 1
  • 15
  • 24
  • What errors are you getting. – StillLearnin Sep 11 '15 at 01:00
  • I have the same problem. Request is sent, but target server didn't get it's body. Did you find any suggestion? – Evgeniy Timchenko Oct 09 '17 at 11:08
  • @EvgeniyTimchenko I do not remember what exactly solved my problem but I've set some more options like (CURLOPT_COOKIEJAR, CURLOPT_COOKIEFILE, CURLOPT_COOKIESESSION, CURLOPT_SSL_VERIFYPEER, CURLOPT_AUTOREFERER, CURLOPT_USERAGENT, CURLOPT_POSTFIELDS and CURLOPT_HTTPHEADER) – Luciano Marqueto Oct 10 '17 at 12:35

4 Answers4

1

I think I know your issue. Try adding

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

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

UnstableEagle
  • 562
  • 2
  • 16
  • @LucianoMarqueto I see that you have `curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);`, however for sake of troubleshooting, try replacing your `1`'s with `true`'s. Do you have any errors that you're getting form the page? – UnstableEagle Sep 11 '15 at 17:22
  • @LucianoMarqueto Does anything display on the page? Any errors? – UnstableEagle Sep 11 '15 at 22:53
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.

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

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

    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.

dlporter98
  • 1,590
  • 1
  • 12
  • 18
0

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

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

Community
  • 1
  • 1
Keval Rathi
  • 978
  • 6
  • 21
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.

AlexPalla
  • 191
  • 3
  • 4