0

My C# code is calling my API service (PHP), which needs to redirect the request to another url, and process the response before returning it. Here is my C# code:

HttpWebRequest request = null;
WebResponse response = null;
Stream writer = null;

request = (HttpWebRequest)WebRequest.Create(http://www.somewhere.com/proxy.php);
request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=" + this.builder.Boundry;

this.builder.RequestStream.Position = 0;
byte[] tempBuffer = new byte[this.builder.RequestStream.Length];
this.builder.RequestStream.Read(tempBuffer, 0, tempBuffer.Length);

writer = await request.GetRequestStreamAsync().ConfigureAwait(false);
writer.Write(tempBuffer, 0, tempBuffer.Length);
writer = null;

response = await request.GetResponseAsync().ConfigureAwait(false);

And then in proxy.php:

<?php
    // 1. Redirect everything (with the content stream from the C# code) to http://www.elsewhere.com
    // 2. Process the response from http://www.elsewhere.com
    // 3. Return processed data to my C# code
?>

How can I do this? It needs to redirect the request stream that I put on this line:

writer = await request.GetRequestStreamAsync().ConfigureAwait(false);
writer.Write(tempBuffer, 0, tempBuffer.Length);
Drake
  • 2,679
  • 4
  • 45
  • 88
  • http://php.net/manual/en/function.header.php – Matt Apr 04 '17 at 19:37
  • why not just call the elsewhere.com from the start? – James Ralston Apr 04 '17 at 19:40
  • Because `proxy.php` needs to take the response from elsewhere.com and change it before returning it to my C# code. – Drake Apr 04 '17 at 19:44
  • @mkaatman `header` won't work in this case because it redirects the request without letting me change the response before sending it back to the caller. – Drake Apr 04 '17 at 20:07
  • What do you mean by response? Are you doing more than choosing which URL to redirect to? You aren't trying to send anything else back right? – Matt Apr 04 '17 at 20:15
  • elsewhere.com returns a JSON, which I need to change/process (in `proxy.php`) before sending it back to my C# code – Drake Apr 04 '17 at 20:22
  • @Darius why not process it directly in your C# code? Get rid of the middle-man. – MrZander Apr 04 '17 at 20:44
  • @MrZander Because the processing involves calling other APIs, recording history, etc that the C# app should not know about. – Drake Apr 04 '17 at 20:47

2 Answers2

0

You can use location header in PHP to redirect to another page:

<?php
  header("Location: http://www.somesite.com/another_page.php");
  exit();
?>  

No matter the caller program is C# or other languages.

Alireza Zojaji
  • 802
  • 2
  • 13
  • 33
0

You will need to use something like cURL on the PHP side in order to process the result before returning it.

Here is an example of using cURL to get data

function getData(){
    $curl = curl_init("http://thewebsite.com/api/whatever");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}

This will also depend on your PHP configuration, some installations don't have the cURL module installed/enabled.

MrZander
  • 3,031
  • 1
  • 26
  • 50
  • Here is another SO question that has some good cURL examples http://stackoverflow.com/questions/9802788/call-a-rest-api-in-php – MrZander Apr 04 '17 at 20:55
  • Will the content that I placed in the request stream (from my C# code) be included in the curl call? Or do I have to manually add that before executing it? – Drake Apr 04 '17 at 21:02
  • @Darius You will have to manually add it. It is an entirely new API call originating from the server running the PHP script. – MrZander Apr 04 '17 at 21:07
  • Do you know how to add it? – Drake Apr 04 '17 at 21:11
  • @Darius Take a look at that link I commented. `curl_setopt($curl, CURLOPT_POSTFIELDS, $data);` should be what you're looking for when attempting to send POST parameters. – MrZander Apr 04 '17 at 21:13