Reputation: 2425
I am trying to create an API using cURL & JSON. Now In my first PHP file I am sending data as JSON data, Like below:
public function index()
{
$url = 'http://172.24.130.50/testbiz/server/login';
$field_string = "{“Request”: “Login”, “Username”: anonymous, “APIAccessKey”: “AW342FFGRTR56RTH”, “GMT-Timestamp”: “1489670000”}";
$json = json_encode($field_string);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $json);
//execute post
$result = curl_exec($ch);
echo "<pre>".$result."</pre>";
//close connection
curl_close($ch);
}
But on my SERVER where I get this request when I show (using var_export()
) the values:
public function login()
{
$client_data = $this->input->post();
var_export($client_data);
}
Error:
array ( '"{\u201cRequest\u201d:\u201cLogin\u201d,\u201cUsername\u201d:\u201cBIZRTC\u201d,\u201cAPIAccessKey\u201d:\u201cAW342FFGRTR56RTH\u201d,\u201cGMT-Timestamp\u201d:_\u201c1489670000\u201d}"' => '', 0 => '', )
Even if I decode it there I still get \u
. Also I dont understand how are these values appended : at starting : '"{\\u201c
and this \\u201d}"' => '', 0 => '',
at the back.
What are these values?
How do I decode this JSON?
Am I Posting right data to Server?
Updated code
Client.php
public function index()
{
$url = 'http://172.24.130.50/testbiz/server/login';
$field_string = array("reqest"=>"login", "user_name"=> "anonymous", "API_AccessKey"=> "AW342FFGRTR56RTH", "GMT_Timestamp"=> "1489670000");
$json = json_encode($field_string);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $json);
//execute post
$result = curl_exec($ch);
echo "<pre>".$result."</pre>";
//close connection
curl_close($ch);
}
Server.php
public function login()
{
var_export($_POST);
}
Upvotes: 0
Views: 700
Reputation: 1202
if you are storing json string in variable then you don't need the json_encode()
because it is used to convert array to json string.
$field_string = '{“Request”: “Login”, “Username”: anonymous, “APIAccessKey”: “AW342FFGRTR56RTH”, “GMT-Timestamp”: “1489670000”}';
$json = $field_string;
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json))
);
Upvotes: 1
Reputation: 1202
it is because of below line
$field_string = "{“Request”: “Login”, “Username”: anonymous, “APIAccessKey”: “AW342FFGRTR56RTH”, “GMT-Timestamp”: “1489670000”}";
$json = json_encode($field_string);
suppose to be like this
$field_string = array("reqest"=>"login", "user_name"=> "anonymous", "API_AccessKey"=> "AW342FFGRTR56RTH", "GMT_Timestamp"=> "1489670000");
$json = json_encode($field_string);
Upvotes: 1
Reputation: 15141
You need to give array in json_encode
, Instead of string.
Replace:
$field_string = "{“Request”: “Login”, “Username”: anonymous, “APIAccessKey”: “AW342FFGRTR56RTH”, “GMT-Timestamp”: “1489670000”}";
$json = json_encode($field_string);
To:
$field_string= '{
"Request": "Login",
"Username": "anonymous",
"APIAccessKey": "AW342FFGRTR56RTH",
"GMT-Timestamp": "1489670000"
}';
Problems in code:
json
.”
) in json
.json_encode
.Upvotes: 2