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;
Related
I have some data that compressed with gzip in an application from here:
app.myaddress.com/data/api/1.
The data contains several parameters in JSON format like follows:
{
"id": 1,
"data": "abcabcabcabcabc" //this is the compressed data
}
I need to check the compressed data with another 3rd party service, we can just say the address like follows: app2.myaddress.com/check_data/abcabc by API request, but it's needed a header authentication:
{
"content-type": "application/json",
"api-key": 123456
}
app2.myaddress.com will return data JSON format like follows:
{
"name": "hello",
"address": "australia"
}
What I need to do is just checking a data by accessing URL like:
app.myaddress.com/data/api/checked/1
then the controller will process the data include checking through app2.myaddress.com and return the value into app.myaddress.com
Solved by #rafitio:
You can use cURL or Guzzle to access both URL inside your function. Here is the documentation of Guzzle: http://docs.guzzlephp.org/en/stable/request-options.html
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.
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}}
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 send bulk requests to the Piwik tracking api (/piwik.php) and I'm running into a problem. When I send the request (from a PHP script over ajax, curl and from fiddler2), I'm receiving the following:
Debug enabled - Input parameters:<br/>array ( )
token_auth is authenticated!
Loading plugins: { Provider,Goals,UserCountry }
Current datetime: 2013-05-02 16:02:27
The request is invalid: empty request, or maybe tracking is disabled in the config.ini.php via record_statistics=0
My post looks like this:
{"requests":["%3Fidsite%3D1%26url%3Dhttp%3A%2F%2Fexample.org%26action_name%3DTest+bulk+log+Pageview%26rec%3D1"],"token_auth":"mytokenhere"}
Which is the example straight from their website. I've made sure to set the content-type to "Content-Type: application/json" and that my configuration has record_statistics = 1 explicitly defined.
According to the documentation, this should all work, but I'm still getting the empty request. The import_logs.py script also works, so I know that the general bulk importing is not broken, but I'm not sure how to get the program to accept my data. Has anyone had any luck with it?
Thanks!
Perhaps the problem with your request is that your query strings are URL encoded, but they don't need to be since they're part of the POST body.
Your POST should be like this instead:
{"requests":["?idsite=1&url=http://example.org&action_name=Test+bulk+log+Pageview&rec=1"],"token_auth":"mytokenhere"}
See the example at the docs for the Bulk Tracking API: http://piwik.org/docs/tracking-api/reference/#toc-advanced-bulk-tracking-requests
Figured out what was wrong. Their documentation was incorrect in how the request needed to be formatted. First, URL Encoded data was unnecessary. Second, the JSON string needs to look like this:
{
"requests": [
{
"apiv": "1",
"bots": "1",
"idsite": "1",
"download": "",
"cdt": "",
"dp": "",
"url": "",
"urlref": "",
"cip": "",
"ua": "",
"_cvar": {
"1": [
"Not-Bot",
"Mozilla/5.0+(Macintosh;+U;+Intel+Mac+OS+X+10_6_5;+en-US)+AppleWebKit/534.10+(KHTML,+like+Gecko)+Chrome/8.0.552.231+Safari/534.10"
]
},
"rec": "1"
}
]
}
Not all of those pieces of data need to be sent, but that's the format necessary. After that it's just data cleansing.