Receiving Gzipped POST requests - php

I'm sending POST requests to a page that as the following code :
$post = json_decode(file_get_contents('php://input'), true);
$text=$post['text'];
echo gettype($text);
echo $text;
When i'm sending strings under 260 or so bytes, it works fine. But for higher sizes, it just prints NULL. I believe that strings >260b get automatically gzipped with the software I use. How can I retrieve the data I want ?
PS : I'm really new to php

Use
$_POST['text'];
instead of:
post['text'];
Edit: misread question, can you try this:
var_dump(json_decode(file_get_contents('php://input'), true));
and post the output here so we know what the return looks like?

Related

PHP: Wating for JSON to load completly before decoding it

I´m requesting an API like so (Domain is NSFW):
$external_videoid = "ph57fbb82bd33ab";
$string = file_get_contents("http://www.pornhub.com/webmasters/video_by_id?id=".$external_videoid."&thumbsize=big");
$json = json_decode($string, true);
$title = $json["video"]["title"];
echo $string;
But I can´t retrieve any data. $title is empty. The echo gives me:
<html><head>[...]<bodyonload="go()">Loading...</body>[...]</html>
Which might indicate that the JSON I want to retrieve is somehow loading, I guess. Seems pretty clear to me. It seems like file_get_contents has to wait for the site to load. What can I do?
APPEND: Sometimes the request works. Sometimes it doesn´t.
Your code works for me, $title echos as:
I Feel Myself Creature Comforts 1
(lol)
I suppose the connection was ACTUALLY loading when you tried it. The json returns some highly "entertaining" information haha, especially the tags (NSFW obviously)
Be aware pornhub has an API(!): http://www.hubtraffic.com/
Use this code.
$v= "ph57fbb82bd33ab";
$url="http://www.example.com/sample/param?id=".$v;
$data = file_get_contents($url);
$characters=json_decode($data, true);
echo $characters['video']['title'];

PHP Variables not passing to url correctly from API

I'm trying to use the Premium URL Shortener script from codecanyon, I have asked for support but they seem to be a little busy, so the response time is not to quick.
The issue I have is when the API sends the request to the url shortener script with the following shortened query for example purposes:
$short = "http://myurl/api?api=MYAPI&format=text&url=http://myfullwebsite.com/email/quote.php?fullname=$fullname&address=$address&emailaddress=$emailaddress";
Although the variables are being placed in the script correctly using echo function at the end of the script after the api request is sent shows they are correctly inserted like so:
http://myurl/api?api=MYAPI&format=text&url=http://myfullwebsite.com/email/quote.php?fullname=Dan Smith&address=12 Main Street, London&emailaddress=dan#smith.com
However if I click the shortened url provided to me from the script I only get the following url string appear in the browser:
http://myfullwebsite.com/email/quote.php?fullname=Dan
It seems as soon as there is a space or even if there is no second name such as Dan Smith and only Dan is the available name, it will not even apply the second ampersand or & sign.
I have tried to use urlecode() but still no joy and I've been pulling my hair out for the last 3 days!
As a novice beginner it has been somewhat difficult to try and achieve the end result and it seems unreachable so I would appreciate any kind help or advice if possible, Maybe I'm missing something so simple?
I've thought of having the url query build from an array of variables but as a novice I've tried one way and failed so not sure if I have done it wrong.
Here is my full api code where I have tried both with SESSION and GET but that is not the problem as the end result echos to the browser with the variables there.. it's only when you follow the shortened url link that you see they're missing.
<?php
session_start();
$fullname = htmlspecialchars($_GET["fullname"]);
$address = htmlspecialchars($_GET["address"]);
$postcode = htmlspecialchars($_GET["postcode"]);
$emailaddress = htmlspecialchars($_GET["emailaddress"]);
$short = "http://myurl/api?api=MYAPI&format=text&url=http://ukhomesurveys.co.uk/email/quote.php?fullname=$fullname&address=$address&emailaddress=$emailaddress";
echo $short;
// Using Plain Text Response
$api_url = $short;
$res= #file_get_contents($api_url);
if($res){
echo $res;
}
?>
Hope I covered everything and hope I have not confused anyone. Thanks.
I think the good choice here is to encode your query with base64 and then pass it to the shortener. In your http://myfullwebsite.com/email/quote.php you just decode the query and use it. The standart PHP functions are base64_encode and base64_decode.
Did you try to encode URI using rawurlencode?
$url = rawurlencode('http://myfullwebsite.com/email/quote.php?fullname=Dan Smith&address=12 Main Street, London&emailaddress=dan#smith.com');

Retrieve content of another website

I want to get the content of another page. The background is that I wanted to make an AJAX request but due to the Same Origin Policy I cannot do this. Now I wanted to write an own PHP script on which I make the AJAX request. The URL looks like the following:
http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=1&tx_manager_pi9[category]=test&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&cHash=123456789001233455332
I tried it with fopen, curl and file_get_contents. Nothing from the works. The problem is if I put in the URL as string like
$results = file_get_contents('http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=1&tx_manager_pi9[category]=test&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&cHash=123456789001233455332');
it does work. If I put in a variable
$url = 'http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=1&tx_manager_pi9[category]=test&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&cHash=123456789001233455332';
$results = file_get_contents($url);
I come to a wrong page. With the specific parameter I get a result. If the parameter are not given correctly it seems that I come to a default page. I can't make a sense out of it.
The same for curl:
$curlSession = curl_init();
$options = array
(
CURLOPT_URL=>$url,
CURLOPT_HEADER=>false,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_FOLLOWLOCATION=>true
);
curl_setopt_array($curlSession,$options);
$results = curl_exec($curlSession);
This doesn't work. If I put in the URL as string and not as variable I get some results! I thought that the ampersand & or the square brackets [] are the problem but I cannot say this. & should be reserved and [] are no correct URL parameters. But why does the direct input work and not the variable?
I used the variable because I make some replacements with str_replace where I make the query more flexible.
I saw similar questions here (cURL function not working, curl_setopt doesnt work with url as a variable) but there was never posted a real solution.
You have a , instead of a ; in your second code block.
Are you required to be "logged in" to the site that you're visiting? That would explain why it's working in your browser and not through your server script.
If all else is the same, your browser and the PHP functions you listed should return the same results.
Could you provide the actual URL for us to test?
EDIT: Based on the URL you provided, it's working fine for me:
php > $test = file_get_contents("http://www.domain.com/user/user_neu/index.php?id=16518&tx_stusermanager_pi9%5Bindications%5D=1&tx_stusermanager_pi9%5Bcategory%5D=cure&tx_stusermanager_pi9%5Baction%5D=getHousesByIndications&tx_stusermanager_pi9%5Bcontroller%5D=HouseFinder&cHash=88230660f01ads34d73a199b82e976");
php > var_dump($test);
string(29) "16,15,14,13,12,11,17,19,22"
My problem was that I used an encoded URL as starting point. E.g.
http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9%5Bparameter%5D=%23%23%23param1%23%23%23&tx_manager_pi9%5Bcategory%5D=%23%23%23param2%23%23%23&tx_manager_pi9%5Baction%5D=getInfos&tx_manager_pi9%5Bcontroller%5D=Finder&cHash=123456789001233455332
I made a str_replace on a URL encoded string. Even using urldecode afterwards the URL was not correctly generated for curl, file_get_contents, ...
The correct URL should be something like this
http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=###param1###&tx_manager_pi9[category]=###param2###&&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&&cHash=123456789001233455332
i.e. without &, %23, %5B, %5D

php - How to access post variables ($_POST is empty)

I'm working on a system and currently try to implement a script that another (external) system can post to some data so I can store them.
I have no control over the external system - I can just trigger it to post data to my system, giving it my script's url.
Looking at firebug when the post happens, I can see the data posted, something that looks like this:
or (urldecoded)
content={"sex":"male","person":{"name":["chris"],"mbox":["mailto:name.lastename#gmail.com"]}}
&Content-Type=application/json
&auth=DDE233H76BGN
My problem is that when trying to get these parameters in my script, $_POST (and $_REQUEST) is always empty!
I've tried var_dump($_POST) or echo file_get_contents("php://input");, but I don't see any contents.
What am I missing here?
I don't know if response/request headers are needed to get something out of it, I show them here just in case
Edit:
My script now consists of a single line of code, like:
<?php
var_dump($_POST);
?>
or
<?php
echo file_get_contents("php://input");
?>
both of them give me absolutelly nothing :s
The data should be accessed using $arr= json_decode($_POST['content']); ... but you have another problem here.
A detail is missing:
... how can firebug can show you the content of a $_POST that is sent from an external system to your website ( aka: the request does not go through your browser, but probably through an CURL request originating from the external server ). Obviously, I don't get something here.
What I see is a POST request sent from your browser ( in javascript ), made by your website.
Your question miss a crucial detail, I'm just not sure what it is.
Hint:
Try to put an echo 'test'; just before your var_dump, I have the feeling that you may not be debugging the page that is really called by the Ajax POST request that we see in Firebug. A little routing problem ?
Lets look at RFC 1945 to see what "parameter" is
parameter = attribute "=" value
attribute = token
token = 1*<any CHAR except CTLs or tspecials>
CTL = <any US-ASCII control character
(octets 0 - 31) and DEL (127)>
So i suppose "Content-Type=application/json" is not a valid part of POST because "-" is not of CTLs
You should try looking at the raw POST data variable:
echo $HTTP_RAW_POST_DATA;

Can't get 'http put' content in PHP

framework is here
http://luracast.com/products/restler/
i'm using restler as restful api for my work,
when i use backbone model save to a url, it sends and update my data as json by using 'HTTP PUT' request method, and i want to get a response from what i've putted...
if it's a HTTP POST request method i can use
// to getting content from a POST
$post_data = json_decode(file_get_contents('php://input'), true);
to get my content, but cant get anything from HTTP PUT
// can't get anything from a PUT
function putpatients($id) {
$post_data = file_get_contents('php://input');
$post_data = json_decode($post_data, true);
echo $post_data['name'];
}
the browser response blank
how do i return my data as json ???
As I commented on your question, php://input is a stream, if you read from it, it empties it.
I've never used Restler before but looking at the few examples provided in their download, it seems to indicate the submitted data is automatically passed as a parameter to your put handler..
In Restler's crud example, the Author class has a put request like this:
function put($id=NULL, $request_data=NULL) {
return $this->dp->update($id, $this->_validate($request_data));
}
so i'm guessing that restler has already read the php://input stream, and hence emptied it.
so, your put handler should maybe be more like in their example:
function putpatients($id, $request_data = NULL) {
/* do something with the $request_data */
var_dump($request_data);
}
Edit: There's actually a previous SO question from #deceze that talks about why reading twice from php://input doesn't work - for PUT requests - which explains why your code worked with a POST request. Either way, you should really use the facility provided by Restler rather than re-inventing the rest wheel.
Does the developer tool of your choice (firebug etc.) show a response?
If so it could help if you put echo json_encode($post_data['name']); instead of your echo.
try to use print_r() function for displaying the values of the variable example:
print_r($post_data);

Categories