K
K
khodos_dmitry2018-07-31 12:19:33
PHP
khodos_dmitry, 2018-07-31 12:19:33

How to send such a request via curl in php?

curl -X GET --user username:password -i http://yourdomain.com/wp-json/wp/v2/posts?status=draft

I am trying to send like this:
$ch = curl_init();
  $url = "http://yourdomain.com/wp-json/wp/v2/posts?status=draft";
  $postfields = ['user' => 'admin:password'];
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
  echo curl_exec($ch);

but the response is 401 code.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2018-07-31
@Rsa97

--user is a CURLOPT_USERPWD option, not a POST element

L
Lander, 2018-07-31
@usdglander

$url = "http://admin:[email protected]/wp-json/wp/v2/posts?status=draft";

Seems like it used to work too.

S
Sergey Panteleev, 2019-06-24
@s_panteleev

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://yourdomain.com/wp-json/wp/v2/posts?status=draft');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

curl_setopt($ch, CURLOPT_USERPWD, 'username' . ':' . 'password');

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question