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.

5
  • What is your question ? Commented Dec 25, 2013 at 21:30
  • Could you show the responses you are getting? How are they different? Which one is producing the result you are expecting? Commented Dec 25, 2013 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. Commented Dec 25, 2013 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 - <p>The document has moved <a href="dev-test01.servosity.com.lab/admin/">here</…> PHP response also is same but it is redirecting me to different url "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 Commented Dec 25, 2013 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? Commented Dec 25, 2013 at 22:09

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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)
using json_encode is the best practice. but for this case it will get away :-)
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.

4 Comments

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 "}" .
You original example didn't have the comma. Why do you need it? Does this solve the problem for you?
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.
Oh, this is old. but the POSTFIELDS receives it as array?

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.