3

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

3
  • What errors are you getting. Commented Sep 11, 2015 at 1:00
  • I have the same problem. Request is sent, but target server didn't get it's body. Did you find any suggestion? Commented Oct 9, 2017 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) Commented Oct 10, 2017 at 12:35

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

@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?
@LucianoMarqueto Does anything display on the page? Any errors?
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.

Comments

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?

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.