I have the following php web service that writes text (a json object).
This text is then parsed as JSON in javascript code.
$comments = new Attachments();
log(json_encode($comments->toArray()); // log prints correct JSON object
echo json_encode($comments->toArray());
The JSON built is correct, the log is correct.
However, on IIS 6.2 only, the JSON object is invalid, eg:
The JSON object as logged is :
{"someObject": 42, "someOther": "43", "aaa": "bbb", "someKey": "bbb" }
The JSON object as sent by IIS is :
{"someObject": 42, "someOther": "43", "aaa": "bbb", "someKey": "bbb" }eKey: "bbb" }
The JSON sent contains a repetition of the end of the json, which produces invalid JSON.
This bug happens 80% of the times.
Do you have any idea on what could be the cause?
Thanks
This was due to VPN issue.
There is no issue if I access the same website with its external URL, outside of our VPN.
Related
I am a new bee to sigfox. I want to receive sensors' data from sigfox server to my webpage. I defined a callback function as in the pic below in sigfox. I want to know how to receive the JSON data in my webpage. My web page is running on a personal apache web server with a public ip.
I am using php. Like normally use a curl call to receive json from any server using its URL. Here in sigfox case, what source i.e. url I specify in curl to retrieve json data from it. OR what mechanism to use. I searched web & forum but didn't find any answer. Sigfox has a nice description about how to receiver data to an email but not for a custom url.
The body of the callback should be something like this
{
"deviceId": "{device}",
"time": {time},
"seqNumber": {seqNumber},
"data": "{data}",
"reception": [{ "id": "{station}", "RSSI": {rssi}, "SNR": {snr} }],
"duplicate": {duplicate},
"ack": {ack},
}
Other parts in your config seem ok, just write your endpoint in sigfoxdata.php to handle the data (eg. parse it).
I have build a simple PHP API system.
Now a partner company (* that isn't quite behaving as a partner *) and they are sending us data but since their system is making a lot of requests we need to debug.
The debug data they send us are the following:
But I have no clue might sound stupid but unfortunately yes. How to send this to our system as a JSON object.
Could anyone tell me how I can send this data as a JSON object so I can see how our system responds?
{"requested_info":"pers_code_live","provider_id":"20","provider_group_id":"4","indoor":false,"provider_location":"buiten","StartDate":"14-10-2016 16:00","EndDate":"22-10-2016 23:30","email":null,"lang":"nl","personal_code":"wehavediscount"}
Thanks in advance
EDIT
The info I receive is only readable by :
$JSonData = json_decode(file_get_contents('php://input'), true);
But I have no idea how to send the above mentioned data in a format that I have accept in the manner as described two lines up.
I can create a post variable with a JSON encoded array. That's no biggie. But the problem is the JSon object. I don't know how to send something as a JSon object and re-create their created situation.
The standard way is with json_encode():
$arr = ["first key" => 1, "second key" => "two", "third => [1,2,3]];
echo json_encode($arr);
Which will return
{"first key": 1, "second key": "two", "third": {1,2,3}}
i make one php script who send the data one api after successful submission it will reply with all codes which i dont know how to read it. i make one php script who send the data one api after successful submission it will reply with all codes which i dont know how to read it.
{
"ErrorCode": "000",
"ErrorMessage": "Success",
"JobId": "b770e8c9-118b-4347-9708-fcd1176c51d7",
"MessageData": [
{
"Number": "918237020012",
"MessageParts": [
{
"MsgId": "918237020012-9be10ca3793b4e7c83c74d3dfbbfd60d",
"PartId": 1,
"Text": "testing completed"
}
]
}
]
}
This "codes" is called Json Object and in your case it's string encoded so you can decode it into a PHP object something like:
$obj = json_decode($response);
and you can access it's attributes like this:
echo $obj->JobId;
furthermore you can refer to the PHP documentation json_decode
They are called JSON Strings. It's a lightweight data-interchange format.
To decode the JSON string in PHP use json_decode. Like this,
$json_object=json_decode($jsonString);
It will return you PHP Object by default and you just need to read the Object properties. Like this,
echo $json_object->json_key;
This whole process of subscriptions for the Instagram API seems to be less than straight forward.
I have some code set up to receive the post data sent when Instagram hits me with a notification of a post from one of my subscriptions. However when I try to view the data, the raw JSON, it posts I can't get at it. If I print_r or var_dump I just get the number 1.
See my code for accessing the data:
// Catches realtime updates from Instagram
if ($_SERVER['REQUEST_METHOD']==='POST') {
// Retrieves the POST data from Instagram
$update = file_get_contents('php://input');
$data = json_decode($update);
var_dump($data); //Outputs 1
print_r($data[0]); //Outputs 1
}
How can I get at the JSON as an array?
This is what the JSON should look like:
[
{
"subscription_id": "1",
"object": "user",
"object_id": "1234",
"changed_aspect": "media",
"time": 1297286541
},
{
"subscription_id": "2",
"object": "tag",
"object_id": "nofilter",
"changed_aspect": "media",
"time": 1297286541
},
...
]
Thanks for any help.
Update 1
I've used PHP to print the HTTP headers. There's content because it show's it's length. Still unable to get at it though. This rules out it being an Instagram issue, I think
If you are using PHP, I guess the simplest way to access input data is using $_GET and $_POST superglobals. In this case, try to var_dump($_POST) and see what you get.
If you get some content from $_POST, you can use json_decode to decode JSON into an array.
You can also try some PHP implementations of the Instagram API, like this one: https://github.com/macuenca/Instagram-PHP-API It will to the work you need.
Woop found the problem and solved it. It's not easy to debug because all of this happens when Instagram hit your page so you don't really see the output.
What I needed to do was create a foreach loop to run through the decoded JSON. After a lot of debugging and head scratching the JSON isn't empty, it just starts with a JSON array.
Anyway here's the code now that works:
// Catches realtime updates from Instagram
if ($_SERVER['REQUEST_METHOD']==='POST') {
// Retrieves the POST data from Instagram
$update = file_get_contents('php://input');
$data = json_decode($update);
foreach($data as $k => $v) // can be multiple updates per call
{
$sub_id = $v->subscription_id; //Contains the JSON values
$user = $v->object_id;
}
}
If you want to see the outputs from $sub_id for example I suggest logging them or email them to yourself for example.
I'm trying to figure out how to send this kind of data, via PHP, to a RESTful service:
{
"api_version": 1.0,
"project_id" : 123,
"batch_id" : 111,
"accesskey" : "abc123",
"job": {
"file": file,
"word_count": 111,
"title": "FAQ",
"cms_id": "page_123_en_sp",
"url" : "http://original_post_url.com",
"is_update": false,
"translator_id": 123,
"note": "Translate these words",
"source_language": "en",
"target_language": "it"
}
}
First of all, I cannot use curl, as who's going to use this code might not have it installed or might be not allowed to install it.
That's not a 'normal' JSON object.
The job->file property is an actual file (not even an url to a file).
That's the actual code I'm using to send all requests and it worked, until I've met this specific case: http://pastebin.com/6pEjhAkg
The 'file' property is created as such:
$file = generate_file( $content );
protected static function generate_file($content) {
$file = fopen('php://temp','r+');
fwrite($file, $content);
rewind($file);
return $file;
}
Now, when sending data, with $params argument properly set on PHP side, the RESTful returns an error about missing 'api_version' and 'project_id', but they are present.
I'm pretty sure the problem is that he's not receiving data as JSON, but how can I convert to JSON an object that, in his properties, contains a file pointer resource?
The code that sends data and builds the file has been created by a former developer, and I can't get in touch with him.
I tried to understand what is wrong there and the only thing I managed to fix so far, for another unrelated issue, is to actually send JSON data (when $multipart==false), as you can see in lines 16-19 of the linked code, rather than sending urlencoded data.
Any hint?
JSON does not have a "file" concept. You can either load the file content, encode it using Base64 and then stuff it into a JSON string - or you can use multipart format for sending the file in one part and the complete JSON in another part. The multipart solution should be the best performing as it doesn't have to base64 encode the file content.
Here is a similar example message format from Mason that tries to formalize the json/multipart usage:
POST /projects/2/issues HTTP/1.1
Accept: application/vnd.mason+json
Content-Type: multipart/form-data; boundary=d636dfda-b79f-4f29-aaf6-4b6687baebeb
--d636dfda-b79f-4f29-aaf6-4b6687baebeb
Content-Disposition: form-data; name="attachment"; filename="hogweed.jpg"
... binary data for attached image ...
--d636dfda-b79f-4f29-aaf6-4b6687baebeb
Content-Disposition: form-data; name="args"; filename="args"
Content-Type: application/json
{
"Title": "Hogweeds on the plaza",
"Description": "Could you please remove the hogweeds growing at the plaza?",
"Severity": 5,
"Attachment":
{
"Title": "Hogweed",
"Description": "Photo of the hogweeds."
}
}