Skip to content
Advertisement

Write Post data to file with PHP

I need to post data using the code below, to php file that will save it in a text file. I just don’t know how to create the php file to receive the data below and save it in a text file. as simple as possible.

try {  
    // Add your data  
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
    nameValuePairs.add(new BasicNameValuePair("stringData", "12345"));  
    nameValuePairs.add(new BasicNameValuePair("stringData", "AndDev is Cool!"));  
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  

    // Execute HTTP Post Request  
    HttpResponse response = httpclient.execute(httppost);  
    String responseText = EntityUtils.toString(response.getEntity()); 
    tv.setText(responseText);             
} catch (ClientProtocolException e) {  
    // TODO Auto-generated catch block  
} catch (IOException e) {  
    // TODO Auto-generated catch block  
}  

Advertisement

Answer

Simple as:

file_put_contents('test.txt', file_get_contents('php://input'));
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement