Other Cant use file_get_contents("php://input")

MarcoRome

Expert
Licensed User
Longtime User
Hi all.
Until today i have used the db.php file as an example of Erel ( Connect Android to MySQL ) , and it always worked.
Now i have a server that for safety reasons have disabled the use of file_get_contents. I tried using CURL, but without success.

B4X:
<?

$hst  = "localhost";
$db = "xxxx";
$usr = "xxxxx";
$pwd = "xxxxx";

$conn_DB=mysql_connect($hst,$usr,$pwd) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");

$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'php://input');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$query = curl_exec($ch);
curl_close($ch);

// display file
echo $query;


//$query = file_get_contents("php://input");

$sth = mysql_query($query);
...

Any suggestions ??
Thank you
Marco
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
try

PHP:
$fp = fopen("php://input", 'r+');
while (!feof($fp)) {
  $contents .= fread($fp, 8192);
}
fclose($fp);
echo $contents;
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
try

PHP:
$fp = fopen("php://input", 'r+');
while (!feof($fp)) {
  $contents .= fread($fp, 8192);
}
fclose($fp);
echo $contents;

Thank you Don, but this server have "allow_url_fopen" in Off ( disable fOpen ).
I think that only way is cURL. But any you see my code in #1 Post dont work ( i think because wait POST )
Another idea ?
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Only one.
DONT use posttring, use a regular POST method (see multipartpost)

Edit: one more

You could change to another hoster :D
:D The second way is better.
But this app and for a client and i dont think he wants to change :rolleyes::confused:
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
[SOLVED]
if you have a server where it is not enabled fopen, file_get_contents, etc ( in general have "allow_url_fopen" in Off - look php.ini )
This is solution

B4A code:
B4X:
Sub ExecuteRemoteQuery(Query As String, JobName As String)
    Dim job As HttpJob
    job.Initialize(JobName, Me)
    job.PostMultipart("http://www.devil-app.eu/database/dbpwtestmultipart.php", CreateMap("Query":Query), Null )
End Sub

PHP Code:
B4X:
<?

$hst  = "xxxxx";
$db = "xxxx";
$usr = "xxxxx";
$pwd = "xxxx";
$query= $_POST["Query"];


$conn_DB=mysql_connect($hst,$usr,$pwd) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");


//$query = file_get_contents("php://input");
$sth = mysql_query($query );


if (mysql_errno()) {
    header("HTTP/1.1 500 Internal Server Error");
    echo $query.'n';
    echo mysql_error();
}
else
{
    $rows = array();

        while($r = mysql_fetch_assoc($sth)) {
            $rows[] = $r;
        }
   
    print json_encode($rows);
}
?>

That all.
Marco
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Hi Don :D
I looked multipartpost but i dont understand what you mean
Pls, which your solution ?
Thank you for your time
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Pls, which your solution ?
Thank you for your time
No, you maybe misinterpreted my answer.

Just did it RIGHT the way i suggested!

My suggestion was to change the hoster or
DONT use posttring, use a regular POST method (see multipartpost)

Your solution
B4X:
job.PostMultipart("http://www.devil-app.eu/database/dbpwtestmultipart.php", CreateMap("Query":Query), Null )
together with
B4X:
$query= $_POST["Query"];
is EXACTLY what i was suggesting...
 
Upvote 0
Top