How do I query a specific field from JSON response? - php

I'm working on a cURL script to communicate with an API. I wrote a script to retrieve the data with a cURL request and the data is displaying as follows. This is just one chunk of the JSON response as there are usually multiple users online. The unique identifier is ["fromAddress"]=> string(18) "127.0.0.1:5060" which is constant no matter what. The CALLID field is unique every time a call is initiated from the server which makes it a real pain!
What I need to with the JSON Response is this:
Get [CALLID] from array# where ["fromAddress"] equals"127.0.0.1:5060"
Save to a php variable.
I'm not sure what the next step is. Can someone please point me in the right direction?
Thanks!
array(35) { ["callID"]=> string(22) "U1A7B9F7T61A2BC05S2eI1" ["callType"]=> string(3) "sip" ["participantID"]=> int(2) ["started"]=> int(15551212) ["updated"]=> int(15551212) ["name"]=> string(9) "TEST CALL" ["notes"]=> string(0) "" ["toNumber"]=> string(12) "+15551313" ["fromUri"]=> string(58) "sip:5551212#127.0.0.1:5060;pstn-params=908481808882" ["fromAddress"]=> string(18) "127.0.0.1:5060" ["fromName"]=> string(15) "WIRELESS CALLER" ["fromNumber"]=> string(12) "+15551212" ["location"]=> string(14) "SOMEWHERE, CO, US"

you posted a result from var_dump and is not complete
usually when receive json from a response you need to convert it to php's object using json_decode since response always return a string instead an object.
$obj= json_decode($response)
then you can access the data like thia
$obj['callID']
$obj['fromAddress']

Related

GET attribute are ignore while requesting data from WooCommerce REST API

I have a CRM set to crm.example.com And a active store on example.com (on the same server)
Both running with NGINX.
Im trying to request the whole categories list (43 categories) but i'm getting only 10. (The default value set by WooCommerce).
I tried using the per_page parameter but it seems like the server is ignore the GET.
I checked the request sent from crm.example.com and got that:
object(Automattic\WooCommerce\HttpClient\Request)#85 (5) {
["url":"Automattic\WooCommerce\HttpClient\Request":private]=>
string(67) "https://example.com/wp-json/wc/v2/products/categories/?per_page=100"
["method":"Automattic\WooCommerce\HttpClient\Request":private]=>
string(3) "GET"
["parameters":"Automattic\WooCommerce\HttpClient\Request":private]=>
array(1) {
["per_page"]=>
int(100)
}
["headers":"Automattic\WooCommerce\HttpClient\Request":private]=>
array(2) {
["Accept"]=>
string(16) "application/json"
["User-Agent"]=>
string(32) "WooCommerce API Client-PHP/1.3.0"
}
["body":"Automattic\WooCommerce\HttpClient\Request":private]=>
string(0) ""
}
But yet there is only 10 results.
i'm thinking maybe the NGINX server is ignoring the GET parameters as a security policy?

Fetching JSON object out of HTTP request

Folks I think I may miss a dead simple thing but I just have no idea where to look for it.
So I'm trying to implement block.io API to accept bitcoin payments on a website built on PHP. Now the Docs say that all notification events will use JSON objects pushed to my server through POST requests. So I decided to make a test BTC payment and log all requests on a specified URL to my DB. And here what I gen on dumping getallheaders()
{
["Content-Type"]=>
string(16) "application/json"
["User-Agent"]=>
string(12) "Block.io/0.1"
["Accept"]=>
string(10) "text/plain"
["Authorization"]=>
string(10) "Basic Og=="
["Connection"]=>
string(5) "close"
["Host"]=>
string(17) "site.com"
["Content-Length"]=>
string(3) "462" }
But dumping $_POST gives me an empty array. Dumping $_SERVER and $_GET gives nothing useful either.
So my question is how can I find the JSON string declared in Content-Type?
Any suggestion would be helpful!
Thanks!
Because the Content-Type is application/json (not application/x-www-form-urlencoded)
$inputJSON = file_get_contents('php://input');
EDIT: excellent explanation at PHP "php://input" vs $_POST

Reddit More Children API

I have a site where I fetch reddit comments using Guzzle and Reddits json api. While it tends to be really slow, it works, but I've run into an issue when trying to load more children via the /api/morechildren.json endpoint.
I always receive a 403 forbidden error when trying to get more children. These are the params I am using including the data and the endpoint which I am hitting.
array(2) {
["request"]=>
string(44) "https://www.reddit.com/api/morechildren.json"
["data"]=>
array(6) {
["link_id"]=>
string(10) "t3_cz36xw7"
["id"]=>
string(10) "t1_cz361ai"
["depth"]=>
string(1) "1"
["children"]=>
string(7) "cz36xw7"
["sort"]=>
string(10) "confidence"
["api_type"]=>
string(4) "json"
}
}
You can see the api I'm trying to hit here: https://www.reddit.com/dev/api/oauth#GET_api_morechildren
No matter what I try, i get 'string(116) "Client error response [url] https://www.reddit.com/api/morechildren.json [status code] 403 [reason phrase] Forbidden"'
Any ideas?
Authentication is needed. OAuth is authorisation library but hard to use without 3rd party library. Look at https://github.com/OAuthSwift/OAuthSwift, it's a good authorisation library and easy to use.

POST requests to php only appearing in $_GET var

I am sending POST data to a php page and receiving it by var_dump($_POST). However the $_POST is always empty and the $_GET contains the values. I'm certain its a POST request and have tested in Postman REST client and cURL. I am using PHP 5.5.3 and Apache 2.2.
Thanks
EDIT:
I'm using the php scripts as an api so currently I am testing using the above mentioned tools to test the request using POST.
For example I am using this cURL request curl -X POST http://dev/project/build/api/registrants.php?hello=there
Contents of my $_SERVER (redacted in areas)
array(25) {
["HTTP_USER_AGENT"]=>
string(11) "curl/7.37.1"
["HTTP_HOST"]=>
string(3) "dev"
["HTTP_ACCEPT"]=>
string(3) "*/*"
["PATH"]=>
string(29) "/usr/bin:/bin:/usr/sbin:/sbin"
["SERVER_SIGNATURE"]=>
string(0) ""
["SERVER_SOFTWARE"]=>
string(67) "Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/0.9.8za DAV/2 PHP/5.5.3"
["SERVER_NAME"]=>
string(3) "dev"
["SERVER_ADDR"]=>
string(9) "127.0.0.1"
["SERVER_PORT"]=>
string(2) "80"
["REMOTE_ADDR"]=>
string(9) "127.0.0.1"
["DOCUMENT_ROOT"]=>
string(25) "/Applications/MAMP/htdocs"
["SERVER_ADMIN"]=>
string(15) "you#example.com"
["SCRIPT_FILENAME"]=>
string(59) "/Applications/MAMP/htdocs/project/build/api/registrants.php"
["REMOTE_PORT"]=>
string(5) "51096"
["GATEWAY_INTERFACE"]=>
string(7) "CGI/1.1"
["SERVER_PROTOCOL"]=>
string(8) "HTTP/1.1"
["REQUEST_METHOD"]=>
string(4) "POST"
["QUERY_STRING"]=>
string(10) "hello=there"
["REQUEST_URI"]=>
string(45) "/project/build/api/registrants.php?hello=there"
["SCRIPT_NAME"]=>
string(34) "/project/build/api/registrants.php"
["PHP_SELF"]=>
string(34) "/project/build/api/registrants.php"
["REQUEST_TIME_FLOAT"]=>
float(1416256385.03)
["REQUEST_TIME"]=>
int(1416256385)
["argv"]=>
array(1) {
[0]=>
string(10) "hello=there"
}
["argc"]=>
int(1)
}
In your <form> tag, make sure the method="POST" attribute is present.
Well, I was having the same problem and I discovered that the error was that I was using the wrong headers and the variables should be passed in the body of the request and not as URL parameters. I was using Postman to generate POST requests and I was getting $_GET filled and $_POST as (array(0)), so I read about $_POST superglobal and and it only gets filled when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request, according to PHP documentation here: http://php.net/manual/en/reserved.variables.post.php. Then, I read the Postman manual and it shows at the print screens that the parameters must be at the body as explained here at the session request body: https://www.getpostman.com/docs/v6/postman/sending_api_requests/requests. As said by steve on his comment on your question, using curl this request should be formatted like this:
curl -X POST --data "param1=value1&param2=value2" https://example.com/resource.cgi

Twitter API - 406 Not Acceptable Response

For a user profile i query the Twitter Search API via:
curl_setopt($ch, CURLOPT_URL, "http://search.twitter.com/search.rss?q=".urlencode($username."+-badword")."");
which worked excellent the last weeks.
Since a few days the Twitter API gets a lot of requests from my site and sometimes responses with a "406 Not Acceptable"
the complete error message is:
array(18) { [0]=> string(27) "HTTP/1.1 406 Not Acceptable" [1]=> string(35) "Date: Fri, 21 Jan 2011 12:31:06 GMT" [2]=> string(10) "Server: hi" [3]=> string(26) "Status: 406 Not Acceptable" [4]=> string(15) "Retry-After: 10" [5]=> string(30) "X-Served-From: slc1-aah-25-sr1" [6]=> string(38) "Content-Type: text/html; charset=utf-8" [7]=> string(45) "X-Served-By: slc1-acm-32-sr1.prod.twitter.com" [8]=> string(55) "Cache-Control: max-age=15, must-revalidate, max-age=300" [9]=> string(38) "Expires: Fri, 21 Jan 2011 12:36:06 GMT" [10]=> string(21) "Vary: Accept-Encoding" [11]=> string(17) "Content-Length: 1" [12]=> string(21) "X-Varnish: 1897557299" [13]=> string(6) "Age: 0" [14]=> string(16) "Via: 1.1 varnish" [15]=> string(45) "X-Cache-Svr: slc1-acm-32-sr1.prod.twitter.com" [16]=> string(13) "X-Cache: MISS" [17]=> string(17) "Connection: close" }
I don't understand the Twitter Doku about this:
http://apiwiki.twitter.com/w/page/22554652/HTTP-Response-Codes-and-Errors says "406 Not Acceptable: Returned by the Search API when an invalid format is specified in the request."
because, when i retry after a few seconds the request works fine again.
Who has an idea how to fix this?
Reason:
From the docs, the supported formats are:
1) json
2) atom
And you're using rss. That's why you're getting the error. The format has to be specified as the extension. Like this:
http://search.twitter.com/search.format
Solution:
Use atom or json format, like this:
http://search.twitter.com/search.json?q=twitter
Full code should look like:
curl_setopt($ch, CURLOPT_URL, "http://search.twitter.com/search.json?q=".urlencode($username."+-badword")."");
The Twitter API returns a whole series of 400 and 500 errors randomly and at a low level. Generally the exact error code is meaningless, since it varies. From my experience a false error happens about 0.1% of the time when you make a request. The only solution is to repeat the request.
If the error is reproducible, check your code. If the error is random and intermittent, write code that repeats the request when an error occurs.
If it gets overloaded, that might be the reason for the error.
What does 'badword' contain? If it's variable it might contain something that is incorrectly encoded.

Categories