Home > ASP.net C#, General, PHP > How to call a PHP Script from ASP.NET C# and View Its response?

How to call a PHP Script from ASP.NET C# and View Its response?

Hi guys,
I was wondering on net abt , how to call a PHP Script from ASP.NET C#, then found a simple solution.
Actually, Its nothing but calling any other ASP.NET C# file.
The script is as follow:
Basically I wanted to send User Id and Account ID and then process the table:

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://rover.geodesic.net/wcv1/php/ProcessTrans.php?userid=" + item.Value + "&accountid=" + item.Key + "&type=stock");

// Set the 'Timeout' property in Milliseconds.
myRequest.Timeout = 600000;//10 mintutes
myRequest.Method = "GET";
HttpWebResponse newStream = (HttpWebResponse)myRequest.GetResponse();

if ((newStream.ContentLength > 0))
System.IO.StreamReader str = new System.IO.StreamReader(newStream.GetResponseStream());
if (str != null) str.Close();

The small changes I did was, I multi threaded the process, as i was taking more time to execute the PHP page “ProcessTrans.php”;
In above example, Data has been sent in “GET”
If you want to Post Data to remote Web Page using HttpWebRequest :

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://rover.geodesic.net/wcv1/php/ProcessTrans.php?userid=" + item.Value + "&accountid=" + item.Key + "&type=stock");
request.Method = "POST";
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string tmp = reader.ReadToEnd();

It might not work if the site you’re trying to stream is protected by authentication. If it’s Windows/NTLM Authentication and your account has privileges on the site, try using:
request.Credentials = CredentialCache.DefaultCredentials;
after the create request.

Ref: http://www.worldofasp.net/tut/WebRequest/Working_with_HttpWebRequest_and_HttpWebResponse_in_ASPNET_114.aspx


Categories: ASP.net C#, General, PHP
  1. Pooja
    March 15, 2012 at 11:51 am

    hello.. what is the url “http://rover.geodesic.net/wcv1/php/ProcessTrans.php?userid=” + item.Value + “&accountid=” + item.Key + “&type=stock” in your case..?
    Thank You…

    • preetul
      March 16, 2012 at 9:08 am

      This is the PHP script page i am calling from asp.net

  2. Pooja
    March 15, 2012 at 12:12 pm

    Hi….I used the same code but its redirecting to php page in stead of executing the page… Please help…Thanks in Advance…

    • preetul
      March 16, 2012 at 9:12 am

      Plz share your code…as per my knowledge it will never redirct to the page, so need to check u r code

  3. Pooja
    April 6, 2012 at 7:34 am

    “http://rover.geodesic.net/wcv1/php/ProcessTrans.php?userid=” + item.Value + “&accountid=” + item.Key + “&type=stock” how u ll come to know that this the url of PHP page? I mean how u ll get the URL of PHP page u want to execute?
    Thank You… Please help…

  4. Pooja
    April 8, 2012 at 7:05 am

    Hi…But how you ll get this PHP page path “http://rover.geodesic.net/wcv1/php/ProcessTrans.php?userid=” + item.Value + “&accountid=” + item.Key + “&type=stock” without executing it..? Please help… Thank You…

    • preetul
      April 9, 2012 at 4:44 am

      Pooja, I am not understanding your issue , you need to share your code with me, only after that I can provide you some solution.

  5. pooja
    April 13, 2012 at 8:01 am

    hi sir, thank you for your reply
    the problem is,
    we are building a project in asp.net.
    in that we have a php page and asp page
    the asp page contains a button
    after clicking this button the php page must be called.

    please help us

    thank you

  6. pooja
    April 15, 2012 at 8:26 am

    the php code is

    function callFb($url)

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

    curl_setopt_array($ch, array(
    CURLOPT_URL => $url,

    $result = curl_exec($ch);


    return $result;

    $url = “https://graph.facebook.com/oauth/access_token?client_id=ur FB client id&redirect_uri=http://apps.facebook.com/name of ur app/&client_secret=ur secret token&code=”Use ur own security code””;

    $access_token = callFb($url);

    $access_token = substr($access_token, strpos($access_token, “=”)+1, strlen($access_token));

    $url = “https://graph.facebook.com/search?access_token=%24access_token&q”.urlencode($_POST[‘search’]).”&type=user”;

    $ret_json = callFb($url);

    $users = json_decode($ret_json, true);
    $x = $users;

    echo ”;
    echo” Search results for “;
    echo $_POST[‘search’]; echo ”

    these are our facebook keys
    App ID: 381207315225908
    App Secret: 43063379e7f03ae2bd8a4c601ec3616e(reset)

    Token key: 381207315225908|iDGpw2ohEixP-YpCmJ83K4MUxyA

    This is the php code which we need to call in asp.net page

  7. pooja
    April 15, 2012 at 8:27 am

    plz reply soon sir

  8. preetul
    April 16, 2012 at 9:10 am

    Hi Pooja,
    Please send m u r ASP.NET code also(The section of the code by which you are calling PHP script).
    Also, does u r php script works fine? (just make sure before proceeding)

  9. April 16, 2013 at 5:59 pm

    Hi Preetul,

    I have a task which is same as your post. But there is a little more in that.
    As we have two web applications, one in asp.net and other in PHP. In both we create user profiles. Now the task is to Create users in Asp.NET application and we need to save the same information in PHP application from Asp.Net app.

    I am using the below code for that but it is not wroking, Can you please look at it and let me know what I am missing.

    CookieContainer cookies = new CookieContainer();
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@”http://localhost/admin/config/popup_user_info_brand.php”);
    request.PreAuthenticate = true;
    request.AllowWriteStreamBuffering = true;
    request.CookieContainer = cookies; // note this
    request.Method = “POST”;

    string boundary = System.Guid.NewGuid().ToString();
    string Username = “admin”;
    string Password = “admin”;

    if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
    request.Credentials = new NetworkCredential(Username, Password);
    request.ContentType = string.Format(“multipart/form-data; boundary={0}”, boundary);

    StringBuilder sb = new StringBuilder();

    sb.AppendLine(“Content-Disposition: form-data; name=\”name\””);

    sb.AppendLine(“Content-Disposition: form-data; name=\”username\””);

    sb.AppendLine(“Content-Disposition: form-data; name=\”email\””);

    sb.AppendLine(“Content-Disposition: form-data; name=\”password\””);

    // This is sent to the Post
    byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString());

    request.ContentLength = bytes.Length;

    using (Stream requestStream = request.GetRequestStream())
    requestStream.Write(bytes, 0, bytes.Length);

    using (WebResponse response = request.GetResponse())
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))

    Note:- PHP web site is a 3rd party, we have no access on code.


    • April 16, 2013 at 8:39 pm

      Actually, I am not being able to log-in in the site using the above code.

  10. preetul
    April 18, 2013 at 8:58 am

    Hi Ginni,
    I am buzy these days, will give u detail feedback later, but for now I can say that due to authentization on PHP website(which is 3rd party) you are not able to login and in that case you cannor go ahead.

  11. Santosh
    April 22, 2013 at 6:36 am

    hi Preetul,
    Actually my project in asp.net that is like e-commerce site.i want user see the product in .net but when user want purchase that product so i want php pages for paypal purpose.so how will be called?can you send me some code for that bcos in .net IIS server and php i have linux..so pleas help me out sir…

  12. July 31, 2013 at 6:02 am

    If some one wants expert view about running a blog then i
    suggest him/her to visit this weblog, Keep up the good work.

  13. pelis antiguas
    September 23, 2014 at 11:14 am

    Hi there exceptional website! Does running a blog similar to this take a massive amount work?

    I’ve very little understanding of coding however I was
    hoping to start my own blog in the near future. Anyhow, should you have any suggestions or tips for new blog owners
    please share. I know this is off subject but I simply
    had to ask. Many thanks!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: