Skip to content

Instantly share code, notes, and snippets.

@wiefunkdai
Last active September 17, 2023 15:41
Show Gist options
  • Save wiefunkdai/a170fb8886f50a970730630e7b14bd2e to your computer and use it in GitHub Desktop.
Save wiefunkdai/a170fb8886f50a970730630e7b14bd2e to your computer and use it in GitHub Desktop.
PHP file for get response from any request server on Android. This code can also be used to receive requests from Ajax-Javascript, HttpRequest-Net Visual Studio, and any form in the form of POST, GET, PUT, DELETE
<?php
/**
* #AndroidAjaxGetSendRequest
* Implementation of JSON Function API based on
* Request POST, GET, PUT, DELETE methods on Android
*
* @author Stephanus Bagus Saputra
* ( 戴 Dai 偉 Wie 峯 Funk )
* @email wiefunk@stephanusdai.web.id
* @link http://www.stephanusdai.web.id
* @copyright Copyright (c) ID 2023 Stephanus Bagus Saputra
*/
$input=file_get_contents("php://input"); // Get request with method RAW
$data = array('status'=>'OK');
$post = (!empty($input) ? $input : array());
if (isset($_POST) && !empty($_POST)) {
$post = $_POST;
} else if (isset($_GET) && !empty($_GET)) {
$post = $_GET;
} else if (isset($_PUT) && !empty($_PUT)) {
$post = $_PUT;
}
// This work only use request with Content-Type: 'application/x-www-form-urlencoded'
if (isset($_FILES) && isset($_FILES['avatar']) && !empty($_FILES)) {
$uploadDir = dirname(__FILE__);
$fileName = $_FILES['avatar']['name'];
$fileUpload = $_FILES['avatar']['tmp_name'];
$pathUpload = $uploadDir . DIRECTORY_SEPARATOR . $fileName;
if (move_uploaded_file($fileUpload, $pathUpload)) {
$post['photo'] = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/' . $fileName;
}
}
$data['content'] = $post;
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment