1

I have a working command line curl command

curl -v -d '{"auth": {"passwordCredentials": {"username": "myusername", "password": "mypassword"}}}' -H 'Content-type: application/json' myurl

I am trying to write equivalent PHP curl command -

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, myurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$data = array('json' => '{auth: {passwordCredentials : {username : myusername, password : mypassword }}}');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
echo $output;

I am having different response for both the calls. I have doubt about setting the json data correctly.

  • What is your question ? – underscore Dec 25 '13 at 21:30
  • Could you show the responses you are getting? How are they different? Which one is producing the result you are expecting? – Floris Dec 25 '13 at 21:37
  • 1
    The value in $data is not a valid JSON string. You need to use quotes just like in the command line curl example. – Gergo Erdosi Dec 25 '13 at 21:43
  • @samitha - My question is, am i doing something wrong while writing equivalent PHP curl command Floris - Command line works fine, command line response -

    The document has moved here.

    PHP response also is same but it is redirecting me to different url "https://dev-test01.servosity.com.lab/auth/login/?next=/admin/" which shows that in command line it gets authenticated but in PHP it is not getting authenticated and redirecting me to login page in place of profile page. Thanks
    – user3135240 Dec 25 '13 at 22:04
  • Yes that makes sense; you are not forming the request properly. Did the solution in my answer not solve it for you? – Floris Dec 25 '13 at 22:09

2 Answers2

1

Along with your curl request, also send the HTTP header. For example:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

And the post data should be:

$post_data = '{auth: {passwordCredentials : {username : myusername, password : mypassword }}}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • 1
    Good call about the header; not sure you can get away with `JSON` without the double quotes (see how `json_encode` did things in my answer) – Floris Dec 25 '13 at 22:35
  • 1
    using json_encode is the best practice. but for this case it will get away :-) – Sabuj Hassan Dec 25 '13 at 22:39
0

You can use the json_encode function to make sure that things are properly encoded.

See for example https://stackoverflow.com/a/4271654/1967396 .

In your case, it might look like this:

$authData = array(auth =>  array(passwordCredentials => array( username => floris, password => secret )));

and then you create the POST data with

curl_setopt($ch, CURLOPT_POSTFIELDS, array('json'=>json_encode($authData)));

If you do

print json_encode($authData);

you get

{"auth":{"passwordCredentials":{"username":"floris","password":"secret"}}}

Presumably you could do this manually, without the json_encode function.

Community
  • 1
  • 1
Floris
  • 45,857
  • 6
  • 70
  • 122
  • Thank you floris, i did not thought of this approach By the way i wanted json string as - {"auth":{"passwordCredentials":{"username":"floris","password":"secret"},}} "," after the first "}" . – user3135240 Dec 25 '13 at 22:18
  • You original example didn't have the comma. Why do you need it? Does this solve the problem for you? – Floris Dec 25 '13 at 22:23
  • i figured out , i don't need that. Thanks. It did not solved my problem completely as i am still getting same response but the part for which i am here (setting the json data correctly) is solved. – user3135240 Dec 25 '13 at 22:29
  • Oh, this is old. but the POSTFIELDS receives it as array? – Jovylle Jul 02 '22 at 04:53