I am working with an API allows me to send a text message to a particular number it will then post that text info to my server as a JSON response. It has me configure the call back url and it is posting something to it. This is what I am being told the call looks like:
POST /business/getSMS HTTP/1.1
x-hookmobile-message-id: 297173743
User-Agent: Jakarta Commons-HttpClient/3.1
Host: testing.com
Content-Length: 152
Content-Type: text/plain; charset=UTF-8
{
"timestamp":"Fri Jan 18 21:56:32 GMT 2013",
"text":"Test",
"from":"+11111111111",
"messageId":"297173743",
"type":"incomingSms",
"recipient":"+11111111111"
}
What I am having trouble figuring out is how in PHP I can grab the JSON response. I tried $_POST just to see if the posted data is there but I don't get anything. This is the first time I have done this so I am at a bit of a loss here. How do I grab the posted json?
You'll have to read the raw post data.
$postdata = file_get_contents("php://input");
$obj = json_decode($postdata);
//$obj->timestamp == "Fri Jan 18 21:56:32 GMT 2013" etc
Related
I want a the value of the "token" from the json array from the response i get after performing a cUrl post request. after print_r($response); it prints out all that. but i just want the json string and one particular value with php.
Here is my response. I don't want all this before the response
HTTP/1.1 200 OK<br/>
Content-Type: application/json<br/>
Content-Length: 312<br/>
Connection: keep-alive<br/>
Vary: Accept-Encoding<br/>
Status: 200 OK<br/>
Cache-Control: max-age=0, private, must-revalidate<br/>
Date: Wed, 22 Mar 2017 12:52:25 GMT<br/>
Strict-Transport-Security: max-age=31536000<br/>
X-Request-Id: 9418df03bea4e4884522b703d0eec504<br/>
X-UA-Compatible: IE=Edge,chrome=1<br/>
ETag: "dd4de4d3a6e4e499d6a034ce784d2d76"<br/>
X-Runtime: 0.633173<br/>
X-Content-Type-Options: nosniff<br/>
X-Rack-Cache: invalidate, pass<br/>
X-Powered-By: Phusion Passenger 5.0.28<br/>
Server: nginx/1.10.0 + Phusion Passenger 5.0.28
{"response_code":"00","response_text":"Mobile wallet payment request has been issued.","description":"You will receive a bill prompt shortly on your number 0546653444 with invoice no. 201562656, kindly complete it.","transaction_id":"DTV408402","token":"8268dfffa46a16b0665a76","mobile_invoice_no":"201562656"}
You need to tell cURL that you don't want the header:
curl_setopt($ch, CURLOPT_HEADER, 0);
you can use php's explode() function on this response. Or you can adjust your curl request to not return headers with your requests.
$split=explode("\n\n",$response);
$array=json_decode($split[1],true);
var_dump($array);
if this does not work, modify explode function to split on "\r\n\r\n" instead of "\n\n";
you can do it with JSON decode.
$json = $Your_respone_variable;
$obj = json_decode($json);
print $obj->{'token'}; // token can be replaced by any other key of your JSON
For more informations take a look at:
http://php.net/manual/de/function.json-decode.php
I am using an api to get scorecard of matches using php, response is coming from api as text/html so I conveted this response to json using json_encode first and then converted to json_decode for display but I am not getting matchId wise results.
API RESPONSE HEADERS
Connection: keep-alive
Content-Type: text/html
Date: Tue, 09 Dec 2014 13:36:11 GMT
Server: Mashape/5.0.5
Transfer-Encoding: chunked
X-Powered-By: PHP/5.2.17
demo: click here
var_dump($response);
Code Here
$html = json_encode($response);
$json = json_decode($html);
foreach($json as $data => $value)
{
echo $value->matchId;
}
above var_dump giving response, foreach loop printing nothing.
demo link scroll page at bottom you should see '------------' this message. foreach loop print start here
You need to specify if you want to get an associative array and not an object from json_decode . Therefore, you should use
json_decode($response, true)
For more information see here
I am trying to use the Google Flight RPC but am having trouble building a json array to post to it. The only documentation I've found is here. It describes what needs to be sent but doesn't explain how to construct the json array. Specifically, using PHP, I'm not sure how to build and post a json array using the following example from part 1 of the linked documentation.
[,[[,"fs","[,[,[\"SJC\"]\n,\"2012-04-05\",[\"EWR\",\"JFK\",\"LGA\"]\n,\"2012-04-12\"]\n]\n"]
]
,[,[[,"b_ca","54"]
,[,"f_ut","search;f=SJC;t=EWR,JFK,LGA;d=2012-04-05;r=2012-04-12"]
,[,"b_lr","11:36"]
,[,"b_lr","1:1528"]
,[,"b_lr","2:1827"]
,[,"b_qu","3"]
,[,"b_qc","1"]
]
]
]
The above does not appear to be properly formatted to be put into a json array. Additionally, HTTP request headers are needed. I assume these are set via cURL? I'm not clear on the documentation on how to do this.
I've tried several attempts and I don't get back any of the responses that are shown in the documentation.
Edit: Awesome, I got a Tumbleweed badge for this question! I would appreciate help. Thanks.
I was not able to find any documentation either.
However, if you go to https://www.google.com/flights/, open chrome console or firebug, click the network tab, then run a search, you will see that headers and body it makes for the RPC POST.
Sample Headers from the transfer:
Request URL:(I BROKE THE LINK) GOOGLE(dot)com /flights/rpc
Request Method:POST
Status Code:200 OK
Request Headers
:host:(I BROKE THE LINK) GOOGLE (dot) com
:method:POST
:path:/flights/rpc
:scheme:https
:version:HTTP/1.1
accept:/
accept-encoding:gzip,deflate,sdch
accept-language:en-US,en;q=0.8
content-length:169
content-type:application/json; charset=UTF-8
cookie:PREF=ID=f472fc4bbb95bc2b:U=9da5b7e4c1d04bda:FF=0:LD=en:TM=1390684154:LM=1390749713:GM=1:S=orUAMb3qaxBh99PJ; HSID=AHlw351sj7B7Om0t_; SSID=AKycPxLzyXkc4_tZJ; APISID=xKH5zAdc9vfBtiDy/Ab5TlD_Z4w2nP64Wl; SAPISID=7awo9qDssc3wr-fN/AQYOdvCN-I-UwtXQ1; NID=67=XnUn_DGdQDaeczlvXe-qTy9vy8gnQwhFwfRi52TRFS-_Dg-J58CgTGUY6Tkn3cCJYCcVJhK8unOrdffpgzeKed2jPqSazVI4Xplo5fW8-6wXoNi97L2gdoaOms0dKj4iOODoZpzd4DG_8YdQQcH6fl5xY__N929CJr8pdcAUwgnKf8X_mI8sLSB7CKVyS4ZvbGMCAiMLwIs1gJJz-UbppSj; S=travel-flights=5OJmMrbJoqLfOFzkZy285A; SID=DQAAAM0AAAAIGD56aXyxAxrRCSROmPy8AEtV3DaEwKT48aaZ98S35Nss09ishDZ3RxNT6ksikfAOJo-MLYVodF3jr-6imwzC8tRd7cxe-OoyafCZiGaf0qhp-yza4VZlAMInxGPhVae7wSXCRXlqb-wbYHBCHUSz_K5kYpvKwqC8pWuQ_6AUZa3WWqB6OmYpxuihxn3UxSve95zpkziyaDX0JFzUjyWX-0O_iIWZiEztywwyKVWCVv27ByGjIYTYV1G2byExt5M9-kEFpE_v0x8KgU7vleT
dnt:1
origin:(I BROKE THE LINK) SSL GOOGLE(dot)com
referer:(I BROKE THE LINK) SSL GOOGLE(dot)com flights
user-agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36
x-gwt-cctoken:ADS25WMm8S7W0MlpX1-Lf_yNzQCrke7t6OvH2kFLkBJIH_Q-YTuu8VSHmgIxzFtGaL87SsM5PcZECRBP7IqMCbM5QKFVdWrw9hRIkHoL5oiyCzEu2ZCnKuhqvv2sUKcg4Z_HnajCZmM7aQ9nYsVMQnFxqrkgB2Cz7rAIP47zPJ_rakoyxlGE4yJvcuUeiQ
x-gwt-module-base:https://www.google.com/flights/static/
x-gwt-permutation:C8210E5F468630F84E578D8EDE10A1A0
Request Payload
[,[[,"no","[]","1531191655318648",11]],[,[[,"b_al","no:74"],[,"b_ahr","no:s"],[,"b_ca","103:34541"],[,"b_pe","4F2F79B9E3FB0.A40E22A.71A7"],[,"b_qu","0"],[,"b_qc","1"]]]]
Response Headersview source
alternate-protocol:443:quic
cache-control:no-cache, no-store, max-age=0, must-revalidate
content-encoding:gzip
content-length:75
content-type:application/json; charset=utf-8
date:Sat, 22 Feb 2014 05:00:17 GMT
expires:Fri, 01 Jan 1990 00:00:00 GMT
pragma:no-cache
server:GSE
status:200 OK
version:HTTP/1.1
x-content-type-options:nosniff
x-frame-options:SAMEORIGIN
x-xss-protection:1; mode=block
So, what you will likely have to do to get this to work correctly is to make a GET on the path /flights and read the headers, then put the headers in your POST request. (I have had to do similar things in the past). In order to figure out what fields are what I would play around with selecting different fields and seeing what changes in the JSON data that it posts.
I am using the campaign monitor's api and trying to retrieve the response in a json format, which it is supposed to do. I echoed the response, here's an example :
string(342) "HTTP/1.1 400 Bad Request
Server: csw
Cache-Control: private, s-maxage=0
X-CS-Node: 25
Content-Type: application/json; charset=utf-8
P3P: CP="OTI DSP COR CUR IVD CONi OTPi OUR IND UNI STA PRE"
Date: Sun, 27 Jan 2013 12:35:10 GMT
Connection: close
Content-Length: 66
{"Code":250,"Message":"List title must be unique within a client"}"
I would like to get the only last line. How can I do that ? (I tried to explode the response with a white space, but it didn't change anything).
Edit : the documentation is here, I call a method method(), that returns the response $response = method() and I run echo($response)
You could use the preg_match function to search for the JSON by searching for the Regular Expression '/{.*}/'.
preg_match('/\{.*\}/', $response, $matches);
Based on this example code you should be doing:
$result = method(); # I'm assuming you over-simplified the code in your question and "method" isn't the real method name
echo $result->response; # Not echo $result;
I'm trying to edit and tweak someone else's REST server in PHP. It's based on the REST Server written by Phil Sturgeon. Pretty much got my head around all of it, but my requests aren't working as expected.
In the server constructor is the code
switch ($this->request->method)
{
case 'post':
$this->_post_args = $_POST;
$this->request->format and $this->request->body =
file_get_contents('php://input');
break;
}
I know that php://input can only be read once, so doing var_dump(file_get_contents('php://input')) before setting the variables shows that my XML data is being read correctly from the input stream but obviously the variables aren't set right.
But doing var_dump($this->request->body) only outputs NULL! Is there a special technique to storing the contents of php://input in a variable?
EDIT:
I'm using API Kitchen to send the POST request and the headers that it sends are
Status: 200
X-Powered-By: PHP/5.3.2-1ubuntu4.11
Server: Apache/2.2.14 (Ubuntu)
Content-Type: application/xml
Date: Fri, 10 Feb 2012 11:00:43 GMT
Keep-Alive: timeout=15, max=100
Content-Length: 936
Connection: Keep-Alive
I can't see from this what the encoding is.
EDIT 3:
The encoding is application/x-www-form-urlencoded which could be where the problem lies!! How do I specifically say what this should be?
EDIT 2:
$this->request->method contains 'post'
Thanks for all the help, it turns out that in order to work, the content type of the request must be application/xml, not application/x-www-form-urlencoded as it was.
if $this->request->format evaluates to false or NULL or 0, the later part of and operator does not execute.
$this->request->format and $this->request->body = file_get_contents('php://input');
^
|
+--- this part wont execute
You should have written it like
if($this->request->format){
$this->request->body = file_get_contents('php://input');
}
This helps in debugging.