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
Related
Getting response from one of the soap function php, after that sending this response as a parameter to another soap request, that time I need to extract the set-cookie from the previous response header here below my response header. I need to read set-cookie JSESSIONID element ?
HTTP/1.1 200 OK
Set-Cookie: **JSESSIONID=9A977E193F9B505B084D95C3028BAD0C.srv1816**;
Path=/webservices
Content-Type: text/xml;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 15 Apr 2019 07:38:52 GMT
Set-Cookie: aeroID=12988141315553139361555313936504655;Path=/;
Expires=Sun, 09-Feb-2020 07:38:56 GMT
You could just read your JSESSIONID cookie like so :
First get your cookies :
import com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport
def cookieStore= HttpClientSupport.getHttpClient().getCookieStore()
Then just find the one that interest you
def cookies = cookieStore.getCookies()
def jsessionidCookie
cookies.each {
if(it.name == "JSESSIONID")
jsessionidCookie= it
}
I have a Laravel RESTful API setup that needs to return data in JSON format to an app on one of my subdomains ( and thus not within the Laravel app itself ).
In the REST I am returning like:
return \Response::json(array(
"status" => "success",
"type" => "client",
"message" => "Nothing to see here!"
));
In my Class on the subdomain app I am simply trying to return this response to a view print the output for testing.
I would hope that I just get back JSON, but when I do a simple:
echo $resp;
I get nothing
If I do a:
print_r( $resp );
or
json_decode( $resp );
I get:
HTTP/1.1 200 OK
Server: nginx/1.6.2
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.4.35
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Date: Sat, 06 Dec 2014 04:18:15 GMT
Set-Cookie: laravel_session=eyJpdiI6Ik5DdDFGK2tJQ3ZCVkJPQnRtZHRaU2c9PSIsInZhbHVlIjoiT2tXSTJWYzRKUXZncG9NOFZBYkhOUTZRaUg1c3NmUXlHTjF5bzYyaTZRYUY4S3VBMEdwdjdwdXNUNHkwblppV1I5YUFsY2dodGNyRHh5SmZYeGFjcWc9PSIsIm1hYyI6Ijc0Mzg3MGNlNTBiYTEyNTE3MzNhZDVjNjUzNzMwNzk0ZTMwN2MwYmFiNDIyMGE0N2MyNTQxMDNlMDdiOGFmOTQifQ%3D%3D; expires=Sat, 06-Dec-2014 06:18:15 GMT; path=/; httponly
{"status":"success","type":"client","message":"Nothing to see here!"}
I don't want all of that headers stuff in my response like that. Or I am receiving and trying to parse it incorrectly. What am I doing wrong here?
Ok, this was a dumb mistake not with the REST API, but with the cURL call to the API.
If this ever happens to you just check and make sure you do not have:
curl_setopt($ch, CURLOPT_HEADER, 1);
That will output the headers with the response and mess up your day. I hope this helps others.
My return is like this:
return Response::json(array($data));
Just store array to a variable and call that variable on to the response.
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 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;
I've used a simple file_get_contents function but that didn't get the actual contents (output) of that..
I could not figure the error!!!
Code:
<?php
// $url = $_GET['url'];
// $flv_http_path = urlencode($url);
$flv_http_path = 'http://r12.bhartibb-maa1.c.youtube.com/videoplayback?ip=0.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor%2Coc%3AU0dXSlBSUl9FSkNNN19ITFZB&algorithm=throttle-factor&itag=34&ipbits=0&burst=40&sver=3&expire=1285074000&key=yt1&signature=3E1E4994130745C392FA479F6ACCE5F40E703A2C.A87325A1DCB178B04FD89A9DEEE811CDCB08157C&factor=1.25&id=8b2fd4fd9ac2f09f&st=lc';
echo "----$flv_http_path------";
$data = file_get_contents($flv_http_path);
echo "$data";
if($data)
echo "data is avail";
else
echo "data not available";
// $new_flv_path = dirname(_FILE_).'/flvs/sample.flv' ;
$new_flv_path = '/home/public_html/temp/sample.flv' ;
if(file_put_contents($new_flv_path, $data))
return $new_flv_path ;
else
{
echo "else part ";
return false;
}
?>
I got that url from the response headers of the youtube video
and the headers which i got is
http://v3.lscache1.c.youtube.com/videoplayback?ip=0.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor%2Coc%3AU0dXSlBTVl9FSkNNN19ITVpF&algorithm=throttle-factor&itag=34&ipbits=0&burst=40&sver=3&expire=1285088400&key=yt1&signature=536A81F10AA43A4E015BB05FA182A9A966047C3C.C22269E2E1ECFC2C2DE7A8A45BA2C3DF7CF1EC08&factor=1.25&id=fd61d32bbbd1be5e&
GET /videoplayback?ip=0.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor%2Coc%3AU0dXSlBTVl9FSkNNN19ITVpF&algorithm=throttle-factor&itag=34&ipbits=0&burst=40&sver=3&expire=1285088400&key=yt1&signature=536A81F10AA43A4E015BB05FA182A9A966047C3C.C22269E2E1ECFC2C2DE7A8A45BA2C3DF7CF1EC08&factor=1.25&id=fd61d32bbbd1be5e& HTTP/1.1
Host: v3.lscache1.c.youtube.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090616 Firefox/3.5
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: VISITOR_INFO1_LIVE=9CH-GUrsSEQ; __utma=27069237.1455305642.1275034254.1279868001.1280568792.6; __utmz=27069237.1279868001.5.2.utmcsr=google.com|utmccn=(referral)|utmcmd=referral|utmcct=/landing/youtube/lifeinaday/; watched_video_id_list_kvijayhari=7b1d7ce3852b9aca07a985813b83aaa6WxMAAABzCwAAADFuNzRnSExwU0M4cwsAAAB2ajgxNXlQNDFMQXMLAAAARWNjZ0lLdHVDM1lzCwAAAHFHZFo5elhoQ0ZvcwsAAAB0WXMwTXhvbTRjSXMLAAAAYUdBdDZwNGh0c2NzCwAAAGR2V25wMjdBSGZvcwsAAABtNDBhbG1SQzNzSXMLAAAANjhVT1BhTUtwOTBzCwAAADZnaFUxWDBqdVM4cwsAAABiRy0xYTRsUnlEMHMLAAAAWjh5OFFDRFNUQ29zCwAAADY0T0w3NzhBeUlFcwsAAABzQkl1OWpnSWtwQXMLAAAASllYM08wWEEteWdzCwAAAF95WGxpc0g4dkF3cwsAAABzcXZCSXdDMWxtWXMLAAAAaEMzd09EU0U5MHdzCwAAAGZaODhxaHduTVow; auto_translation=b901c47ed36700682e23d64062529856cwQAAAB0cnVl; PREF=f1=50000000&f2=2000&emt=iceberg&ftuc=32&ems=hd720&HIDDEN_MASTHEAD_ID=brO_JIa6RTI; use_hitbox=72c46ff6cbcdb7c5585c36411b6b334edAEAAAAw; GEO=489e10e70a42c0dfed7513e1895ffe1bcwsAAAAzSU56spxTTJhEAw==; watched_video_id_list=2aa4a241cbdc35137f13b3513ea3e653WwQAAABzCwAAAF9XSFRLN3ZSdmw0cwsAAABpeV9VX1pyQzhKOHMLAAAAd3ZsTUFKLVU2SEVzCwAAAENaQmpoVGQ0WjlN
HTTP/1.0 200 OK
Last-Modified: Sun, 20 Jun 2010 03:59:10 GMT
Content-Type: video/x-flv
Date: Tue, 21 Sep 2010 10:05:34 GMT
Expires: Tue, 21 Sep 2010 16:55:00 GMT
Cache-Control: public, max-age=24566
Content-Length: 4077907
Accept-Ranges: bytes
X-Content-Type-Options: nosniff
Server: gvs 1.0
X-Cache: MISS from localhost.localdomain
X-Cache-Lookup: MISS from localhost.localdomain:3128
Via: 1.0 localhost.localdomain:3128 (squid/2.6.STABLE6)
Connection: keep-alive
Check your URL.
When I put your url in browser it gives nothing so file_get_contents returns an empty string.
You need to check the output of file_get_contents as:
if($data !== false)
instead of
if($data)
I also get a HTTP Response 500. In order to crawl Youtube, you probably would have to spoof the User-Agent of the call and other measures to prevent Youtube from identifying you as a crawler.
It's youtubes way of preventing you from grabbing their flv files automatically.
You can't get the file from your server because the download link (which you got from your browser, or how did you find the flv link) is locked to your browser.
Which is why when someone other than you try to call the link we all get the 403 HTTP forbidden, even with a spoofed user-agent.
Try to use cURL and show the headers, you'll see what I mean.
I get a HTTP 403 at the follwoing Location:
http://r12.bhartibb-maa1.c.youtube.com/videoplayback?ip=0.0.0.0&sparams=id,expire,ip,ipbits,itag,algorithm,burst,factor,oc:U0dXSlBSUl9FSkNNN19ITFZB&algorithm=throttle-factor&itag=34&ipbits=0&burst=40&sver=3&expire=1285074000&key=yt1&signature=3E1E4994130745C392FA479F6ACCE5F40E703A2C.A87325A1DCB178B04FD89A9DEEE811CDCB08157C&factor=1.25&id=8b2fd4fd9ac2f09f&st=lc
Response headers:
Content-Type:text/plain
Date:Tue, 21 Sep 2010 09:59:13 GMT
Proxy-Connection:close
Server:gvs 1.0
Via:1.0 proxy3#XXXXX.sch.uk:8080 (squid/2.6.STABLE19), 1.0 wcsproxy.XXXX.org.uk:8080 (squid/2.6.STABLE19)
X-Cache:MISS from proxy3#XXX.sch.uk, MISS from wcsproxy.XXX.org.uk
X-Content-Type-Options:nosniff
Well, when I tried to load the URL you refer to in $flv_http_path I got:
HTTP/1.1 403 Forbidden
Content-Type: text/plain
Connection: close
X-Content-Type-Options: nosniff
Date: Tue, 21 Sep 2010 09:57:19 GMT
Server: gvs 1.0
In return.
That should give you a clue :)
If that was not the acutal file you were trying to open, and you're not actually trying to scrape youtube you should try wrapping the url in urlencode() edit: But the url is already urlencoded (duh!)
"If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode()." -- http://www.php.net/manual/en/function.file-get-contents.php
The link is empty. Fire the link in your browser and check the sourcecode. there is no data.