Reputation: 1307
I am posting data from Vb.net client to a PHP rest api but for some reason the json_decode is not working on the passed string. The code to post to the server is :
Dim payload As String = "{""key"":""Test"",""bookings"":" & txtUpload.Text & "}"
Dim request As WebRequest = WebRequest.Create(String.Format(baseAPIImportUrl))
' Set the Method property of the request to POST.
request.Method = "POST"
' Create POST data and convert it to a byte array.
Dim byteArray() As Byte = Encoding.UTF8.GetBytes(payload)
' Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded"
' Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length
' Get the request stream.
Dim dataStream As Stream = request.GetRequestStream
' Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length)
' Close the Stream object.
dataStream.Close()
' Get the response.
Dim response As WebResponse = request.GetResponse
' Display the status.
MessageBox.Show(CType(response, HttpWebResponse).StatusDescription)
' Get the stream containing content returned by the server.
dataStream = response.GetResponseStream
' Open the stream using a StreamReader for easy access.
Dim reader As StreamReader = New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd
' Display the content.
' Console.WriteLine(responseFromServer)
MessageBox.Show(responseFromServer)
' Clean up the streams.
reader.Close()
dataStream.Close()
response.Close()
The values that are being passed :
{"key":"91a1522Test",
"bookings":
{"booking":[{"ClassId":"4", "ClassName":"THOASC", "YearWeek":"1751"}]} }
On the PHP side I do :
$bookings = $_POST->bookings
$data = json_decode($bookings,true);
$total = count($data['booking']);
The $total should show 1 as there is 1 item in the booking array but it always is shows 0
Upvotes: 3
Views: 101
Reputation: 151
$data = json_decode($bookings,true);
Here as per the php documentation sencond argument in json_decode function denotes whether return output as an object or as an array
argument 2 = true ( will return array )
argument 2 = false ( will return object , it is the default value)
The json_decode produces the following array
Array
(
[key] => 91a1522Test
[bookings] => Array
(
[booking] => Array
(
[0] => Array
(
[ClassId] => 4
[ClassName] => THOASC
[YearWeek] => 1751
)
)
)
)
Hence it should be accessed like count($data['bookings']['booking'])
Upvotes: 1
Reputation: 17417
$_POST->bookings
- this is your problem. ->
is the object access operator, but in PHP, $_POST
is not an object but an array.
If you were submitting this value as part of form data, you would normally access it via array syntax (e.g. $_POST['bookings']
), but from your VB code, you're actually posting the JSON string as the POST body itself.
In PHP, you can access the raw POST body like this:
$bookings = file_get_contents('php://input');
The rest of your code should then work as usual.
Edit: actually, you've got a typo in there as well. Try
$total = count($data['bookings']);
// or
$total = count($data['bookings']['booking']);
Upvotes: 6
Reputation: 8338
There is a json array there also. Try this.
<?php
$data='{"key":"91a1522Test",
"bookings":
{"booking":[{"ClassId":"4", "ClassName":"THOASC", "YearWeek":"1751"}]} }';
$data = json_decode($data,true);
echo '<pre>';
print_r($data);
$total = count($data['bookings']['booking']);
echo $total;
Upvotes: 1