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;
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 have a web service set up for passbook the error log is logging this error:
Get serial #s task (for device 88a9cbef8e318f61, pass type pass.com.passbook, last updated (null); with web service url https://) encountered error: Server response was malformed (Wrong type object for key serialNumbers in response dictionary. Expected NSArray but found __NSCFString.)
the code in PHP when is not found :
$updatable_passes_payload['lastUpdated'] = '';
$updatable_passes_payload['serialNumbers'] = '';
sendResponse(204, json_encode($updatable_passes_payload), 'text/json');
when is found:
$update_time = date('Y-m-d H:i:s');
$updatable_passes_payload['lastUpdated'] = $update_time;
$updatable_passes_payload['serialNumbers'] = json_encode($types);
sendResponse(200, json_encode($updatable_passes_payload), 'text/json');
the web service json :
HTTP/1.1 200 OK
Connection: close
Transfer-Encoding: Identity
Content-Type: text/json
X-Powered-By: PHP/5.4.22
Server: Apache mod_fcgid/2.3.10-dev
Date: Sun, 01 Dec 2013 23:25:08 GMT
{"lastUpdated":"2013-12-01 23:25:09","serialNumbers":"[\"102058742\"]"}
i checked this json in a json validator online it returns OK
when is not found it returns
HTTP/1.1 204 No Content
Connection: close
Content-Type: text/json
X-Powered-By: PHP/5.4.22
Server: Apache mod_fcgid/2.3.10-dev
Date: Sun, 01 Dec 2013 23:35:17 GMT
for some reason is not returning any json when not found
any help will be appreciated
Your issue is with your JSON array. You have encapsulated your array in quotes so it is being interpreted as a string.
The following should work:
{"lastUpdated":"2013-12-01 23:25:09","serialNumbers":["102058742"]}
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
I am using PHP http_post_data() call to send data to a cakephp controller.I do it like this:
$response=http_post_data($url, $xml_data_encoded);
The data arrives ok to the destination and I get a response which holds the response status.In my case the status is number 1 which means -data delivered ok.As you can see from the code below I get not only the status number (which is at the bottom of the message) but also the whole http post header.How can I strip this message off the header code so that eventually the response message holds only the status number?
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Thu, 10 Nov 2011 08:34:15 GMT
Server: Apache/2.2.17 (Ubuntu)
X-Powered-By: PHP/5.3.5-1ubuntu7.3
Set-Cookie: CAKEPHP=xxxxxxxxxxxxxxxx; expires=Fri, 18-Nov-2011 16:34:15 GMT; path=/XXXXXXXXXX/xxxxxxxx
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
Vary: Accept-Encoding
Content-Length: 19
Content-Type: text/html
1
Btw, I also tried this:
HttpMessage::getBody(http_post_data($url, $xml_data_encoded));
and got no response at all.
Any help will be highly appreciated.
$response = http_post_data($url, $data);
preg_match_all('~HTTP/1\.[01]\s(\d{3})~', $response, $codes);
$codes would store all matches. Just print_r the array and look for desired keys.
Instead of a regular expression you can use http_parse_message
$response = http_parse_message(http_post_data($url, $data));
print $response->body;
Can anyone enlighten me how to create a _$folder$ using Google storage API? Here is the class that I have so far (I managed to list/filter files), but no success with creating a 'directory'. http://guy.codepad.org/lEO4J6hL
When I try to create a test_$folder$, this is what I send to the server:
PUT /test_$folder$ HTTP/1.1
Host: static.hotelpublisher.com.commondatastorage.googleapis.com
Date: Mon, 29 Nov 2010 19:45:49 GMT
Content-Length: 0
Content-Type: application/octet-stream
Content-MD5: 1B2M2Y8AsgTpgAmY7PhCfg==
Authorization: GOOG1 GOOGRVMFQJPKHRXAU3F6:gtzlxexMjBOafn5tOZKF7UZGv1I=
x-goog-acl: public-read
This is what I get in return:
<?xml version='1.0' encoding='UTF-8'?><Error><Code>MalformedHeaderValue</Code><Message>An HTTP header value was malformed.</Message><Date>mon, 29 nov 2010 19:34:23</Date></Error>
This is done following the Google provided documentation, thus I don't see why this does not work.
Hm... have you tried taking out the Content-MD5 header? It doesn't look like it's necessary since your Content-Length is 0.