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);