ervinbosenbacher
ervinbosenbacher

Reputation: 1780

WAMP PHP page not receiving data from C# code

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 :)

Upvotes: 1

Views: 635

Answers (2)

ervinbosenbacher
ervinbosenbacher

Reputation: 1780

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.

Upvotes: 1

Sergey
Sergey

Reputation: 5207

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');

Upvotes: 1

Related Questions