【问题标题】:PHP receive http post request as $_POSTPHP 以 $_POST 形式接收 http post 请求
【发布时间】:2015-03-21 08:00:54
【问题描述】:

我正在尝试使用 $_POST 在 php 中接收一些图像数据,如果我尝试发布像“abc”这样的小字符串,我会收到响应,但是当我尝试发布像 108 KB 这样的大数据时,它可能不会显示响应是我需要增加一些限制,但我无权访问 php.ini 文件。

还有其他方法吗?

我正在从 android 发布数据,所以在 android 中是否有任何字符串缩短编码和 php 中的解码可用。我已经使用base64来获取图像数据了。

我的 php 代码回显返回。

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');


if(isset($_POST['incidentDetails']))
{
    echo 'Responce from server:  ' . $_POST['incidentDetails'];
}
else
{
    echo 'Request without paramenter';  
}
?>

网址 - http://bumba27.byethost16.com/incidentManagments/api/adding_incident_details.php

我需要作为参数名称发布 eventDetails-http://www.filedropper.com/log_2

安卓代码

// Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://bumba27.byethost16.com/incidentManagments/api/adding_incident_details.php");

        try 
        {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("incidentDetails", headerData));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);

            responseCode = response.getStatusLine().getStatusCode();
            responseBody = EntityUtils.toString(response.getEntity());
        } 
        catch (Throwable t ) 
        {
            Log.d("Error Time of Login",t+"");
        } 

【问题讨论】:

  • 如果您正在处理图像文件,为什么要使用 $_POST 而不是使用 $_POST 使用 $_FILES
  • 即使你有很多图像和很多参数,也不需要对图像进行base64编码。
  • it's not showing the response 。不?没有?你在哪里显示响应?你为什么要求字符串缩短代码,其中 base64 将有效负载增加 30℅ ?
  • 我们看不到您发布了 base64 编码图像。只有一个图片网址。我们看不到回应。你没有回答我的问题。您没有说明为什么要对图像进行 base64 编码。你没有告诉你会发布 json。
  • I am getting all parameter as json. ???您的意思是:我将所有参数(包括图像)作为 json 文本发送?

标签: php android post http-post


【解决方案1】:

您是否确保在发送图像数据的 HTML 表单上的表单标签中有 enctype="multipart/form-data"?

<form name="someform" id="someform" enctype="multipart/form-data" method="post" action="somefile.php">

编辑: 根据 PHP.NET 网站,您必须执行一个过程来保存文件 - 而不仅仅是打印出 $_POST 数组:

$name= $_FILES["myfile"]["name"];
$type= $_FILES["myfile"]["type"];
$size= $_FILES["myfile"]["size"];
$temp= $_FILES["myfile"]["temp_name"];
$error= $_FILES["myfile"]["error"];

if ($error > 0)
    die("Error uploading file! code $error.");
else
   {
    if($type=="image/png" || $size > 2000000)//condition for the file
    {
    die("Format  not allowed or file size too big!");
    }
    else
    {
     move_uploaded_file($temp, "uploaded/" .$name);
     echo "Upload complete!"; 
     }
}

【讨论】:

  • 我只在 application/x-www-form-urlencoded 上得到结果 你也可以试试 -bumba27.byethost16.com/incidentManagments/api/… Parameter-eventDetails
  • 我刚刚使用 application/x-www-form-urlencoded 通过表单发送了 Desert.jpg(库存 Windows 背景图像) - 你得到了什么吗?
  • 我没有在数据库中保存任何东西,主要只是在回显结果
  • 您可以使用 Advanced Rest Client 来检查我也更新了我的问题的回复。
  • 我不能像这样上传图像,在这种情况下我还需要更改 android 代码。我需要将其发布为 Base64 我还附上了 url 以及参数.. 如果你不介意你可以检查一下..
猜你喜欢
  • 1970-01-01
  • 2016-10-07
  • 1970-01-01
  • 2015-06-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
  • 1970-01-01
  • 2011-01-02
相关资源
最近更新 更多