1

I am more than convinced that similar questions have been asked here but I am loosing my mind now.

I have a very simple code in C# and PHP that I wrote just to test my environment. There are two parts, the first is the C# code that supposed to post some data and the second, the PHP, which is supposed to receive the data. In the html visualizer of the VS2010 I can see what I am expecting but I am unable to see the same thing in the web browser. I must admit that I am bit newbie as far as the posting to a php page is concerned so any help would be greatly appreciated.

Part 1: The C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            string _url = "http://localhost/data.php";
            string _data = "woooooo! test!";

            for (; ; )
            {
                using (WebClient _client = new WebClient())
                {
                    _client.Headers[HttpRequestHeader.ContentType] = 
                         "application/x-www-form-urlencoded";
                    string _html = _client.UploadString(_url, _data);
                }
            }
       }
   }
}

Part 2: the php code:

<?php 
    echo "bah bah bings";

var_dump(file_get_contents("php://input"));
if ($postdata = file_get_contents("php://input"))
{
    echo "Works";
}
else {
    echo "Doesn't work";
}   
echo($postdata);

?>

And calling the page like this:

http://localhost/data.php

This is what I am trying to achieve: Sending a string to a PHP page and having the PHP Page Display The String

Thank you :)

2
  • Underscores o variables are not needed. You should use them only when making public classes for DLLs. Commented May 17, 2012 at 14:46
  • And the variable is not public Commented May 17, 2012 at 14:47

2 Answers 2

1

but I am unable to see the same thing in the web browser.

You post your data from C# by method POST, browser call your php by method GET, and when GET php don't receive any data from php://input

You have store POST data like

if ($postdata = file_get_contents("php://input"))
{
    echo "Works";
    file_put_contents('mypathstore/data.txt', $postdata);
}
else {
    echo "Doesn't work";
}

at another page

<?php
echo file_get_contents('mypathstore/data.txt');
Sign up to request clarification or add additional context in comments.

4 Comments

I am calling like this the page: _http ://localhost/data.php
You call your php by GET, but C# have POST to your script php, You couldn't see same page in browser.
Ok Sergey. So what do I need to do in order to fix this. The idea here is that I can see the data in the php page whatever is posted by the c# code.
You have to store your post data at DB (or file), and make another page where you will be get stored data from DB.
1

Right. I got this. The above works perfectly fine. My confusion came from the fact that I wanted to see this in the browser, while it is not necessary. It is a PHP script, server side and the purpose of it to insert data into a MYSQL backend. Thansk for all the help though. Works like a charm.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.