1

I have this data

$data = array('username' => 'myname',
              'password' => 'mypass'
           );
$json = json_encode($data);

How can i pass the json as a variable to a url that looks like this

'http://example.com/login/?data='

this is what i have tried and get '400: data not passed' error

$data = array('username' => 'myname',
          'password' => 'mypass'
       );

$data_string = json_encode($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_URL, 'http://example.com/login/?data='); //also 'http://example.com/login/'
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Accept: application/json',
            'Content-Length: ' . strlen($data_string)
        ));
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);

// Exec
$response = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);

2 Answers 2

3

Any data used in the query string should be URL-encoded.

$url = 'http://example.com/login/' . http_build_query([
    'data' => json_encode([
        'username' => 'myname',
        'password' => 'mypass'
    ])
]);

http_build_query() will take an associative array of parameters, and apply all the proper encodings, building the whole query string for you.

The URL returned:

http://example.com/login/data=%7B%22username%22%3A%22myname%22%2C%22password%22%3A%22mypass%22%7D
Sign up to request clarification or add additional context in comments.

8 Comments

@Marth Well, you don't need POST, since you've put all the data in the URL, but here: php.net/manual/en/curl.examples.php#88055 If you actually wanted to POST the data, don't include it in the URL... just send the JSON as the POST body. Much easier to deal with, and then you don't run into any arbitrary length restrictions.
how can i see the status like (400,200 ...) ?
@Marth curl_getinfo($ch, CURLINFO_HTTP_CODE) php.net/manual/en/function.curl-getinfo.php
@Marth I don't know what you're asking at this point. You originally indicated you wanted the data in the URL. Do you want it in the URL or do you want to send it as the POST body? You're sort of doing both, and we don't know what the server side expects so you'll have to determine that.
i want to post the data and get response
|
0

This is possible to do this, you are pretty much there.

$data = array('username' => 'myname',
              'password' => 'mypass'
           );
$json = json_encode($data);

$url = 'http://example.com/login/?data=' . urlencode($json);

This will be a safe url that can do what ever you want with it.

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.