Solved

Could you point a way on how to call a PHP file with parameters from inside a C# code?

Posted on 2016-08-29
12
68 Views
Last Modified: 2016-08-31
Hi Experts

Could you point a way on how to call a PHP file with parameters from inside a C# code?

It isn't needed to receive any value from PHP .

(WF project)

Thanks in advance.
0
Comment
Question by:Eduardo Fuerte
  • 6
  • 3
  • 3
12 Comments
 
LVL 8

Accepted Solution

by:
Prakash Samariya earned 450 total points
ID: 41774703
You can use WebClient to call (any) external URLs, check below code!
using (var client = new WebClient())
{
    var parameter1 = 50;
    var parameter = 100;
    var result = client.DownloadString(string.Format("http://YOURDOMAIN/YOURPMP.PMP?parameter1={0}&parameter2={1}", parameter1, parameter2));
}

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 41774739
We'd really need to know what the PHP code is doing/expecting. Prakash's code works for a GET request, but it will not work if the PHP is expecting a POST. Can you expand on the PHP code?
0
 
LVL 8

Expert Comment

by:Prakash Samariya
ID: 41774760
Could you point a way on how to call a PHP file with parameters from inside a C# code?
It isn't needed to receive any value from PHP .

@käµfm³d, Thanks for comment, but as @Eduardo has already mentioned that parameter will be passed to it, and it is not needed to receive any value from PHP!

You can send form-data in query string and it will work same as POST method in PHP! (I guess), so it won't be a problem

So my solution will work for this case!
1
 

Author Comment

by:Eduardo Fuerte
ID: 41774794
Hi

@Prakash Samariya  
I guess your solution is near to work

C# code:
	private void bttnCLOSE_Click(object sender, System.EventArgs e)
		{

            using (var client = new WebClient())
            {
                var parameter1 = "Bibliotecas";
                var parameter2 = "0";
                var parameter3 = "TRIAL";
                

                var result = client.DownloadString(string.Format("http://www.mysite.com.br/registra.php?parameter1={0}&parameter2={1}&parameter2={2}", parameter1, parameter2, parameter3));

            }
		this.Close();
		}

Open in new window


PHP Code  (registra.php):
<?php

    $sistema = $_POST['sistema'];
    $key     = $_POST['key'];
    $client  = $_POST['client'];


    // Connect to server and select database.
	mysql_connect("$host", "$usermysql", "$passmysql")or die("cannot connect"); 
	mysql_select_db("$db_name")or die("cannot select DB");
    
    $dia = date("d/m/Y H:i:s");
    
    $sql = "INSERT INTO XXXX_login(sistema, casa, cpfc, dia) VALUES ('%s','%s', '%s','%s');";
       
//    echo $sql; 
    
    $comando = sprintf($sql, $sistema, $client, $key, $dia);
	$result=mysql_query($comando);
    
?>    

Open in new window


I don't know why the parameters sent is received as "blank values" accordingly with
(the third line is correct, obtained from another technology system)
img002
Table structure
img001
Any suggestions?
1
 

Author Comment

by:Eduardo Fuerte
ID: 41774834
Changing C# code to

var result = client.DownloadString(string.Format("http://www.mysite.com.br/registra.php?sistema=BIBLIOTECAS&key=0&client=TRIAL")); 

Open in new window


And adapting the PHP code to:

    $sistema = $_GET['sistema'];
    $key     = $_GET['key'];
    $client  = $_GET['client'];

Open in new window


It works fine. Maybe something else must be considered.
1
 
LVL 8

Expert Comment

by:Prakash Samariya
ID: 41774842
                var result = client.DownloadString(string.Format("http://www.mysite.com.br/registra.php?parameter1={0}&key={1}&parameter2={2}", parameter1, parameter2, parameter3));

Open in new window


Replace with below line
                var result = client.DownloadString(string.Format("http://www.mysite.com.br/registra.php?sistema={0}&parameter2={1}&client={2}", parameter1, parameter2, parameter3));

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 41774845
@Prakash
You can send form-data in query string and it will work same as POST method in PHP!
You cannot possibly say that without seeing the code; otherwise, you're just speculating. It all depends on how the PHP is written. If it is expecting POST data, then it won't look in the querystring. The HTTP spec doesn't forbid querystring parameter on POSTs, but generally speaking your "parameters" are in the data. Just because OP says "passed", it does not implicitly mean "passed via querystring."

As you can plainly see in the PHP, it's expecting POST parameters:

e.g.

$_POST['sistema'];
0
 

Author Comment

by:Eduardo Fuerte
ID: 41774852
Sorry.... coding correctly

var result = client.DownloadString(string.Format("http://www.mysite.com.br/registra.php?sistema={0}&key={1}&client={2}", parameter1, parameter2, parameter3));

Open in new window


Works fine!
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 50 total points
ID: 41774853
@Eduardo

If you want to use WebClient, then you'd need to use one of the UploadData overloads. That method will allow you to submit a POST request.
0
 

Author Comment

by:Eduardo Fuerte
ID: 41774855
Our messages crossed... same meaning!
0
 

Author Comment

by:Eduardo Fuerte
ID: 41774885
@käµfm³d 👽

In my case it's not necessary to use POST.
0
 

Author Closing Comment

by:Eduardo Fuerte
ID: 41774888
Thanks for help!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
session dropped in IE 10 20
Paging Using PHP 7 33
How to display a functions data inside HEREDOC 2 14
Codiing Non-Existent Links 4 27
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now