1

I have two scripts:

First to send:

$url = "http://localhost/curl2.php";
$data = array('email' => '[email protected]');
$addr = $url . '?' . http_build_query($data);
$ch = curl_init($addr);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_exec($ch);

And second to receive:

if($_SERVER['REQUEST_METHOD'] == "PUT") {

    $data = array();
    $incoming = file_get_contents("php://input");
    parse_str($incoming, $data);
    echo "Address: " . filter_var($data["email"], FILTER_VALIDATE_EMAIL);
}

But variable $incoming is empty. How can I do it? Maybe it has something to do with PUT, but I must use PUT.

3
  • May be obvious, but is $incoming empty? Commented May 3, 2017 at 17:43
  • Yes, my mistake. $incoming is also empty. Commented May 3, 2017 at 17:45
  • Use CURLOPT_POSTFIELDS instead of appending your fields to the URI (making them GET parameters). Commented May 3, 2017 at 17:48

1 Answer 1

1

PUT is not GET. Use the URL without a query string and add the data to CURLOPT_POSTFIELDS:

$url  = "http://localhost/curl2.php";
$data = array('email' => '[email protected]');
$ch   = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_exec($ch);
Sign up to request clarification or add additional context in comments.

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.