1

Well, I know that the headline look simple, but i was looking from 3 days for an example on how to make the POST request to webapi.

Currently I am using JQuery to do my POST, but I need some php script to run and talk to my C# webAPI, and it seems impossible to find some examples or explain on how to do that.

Someone gave me then Code :

$response = file_get_contents('http://localhost:59040/api/Email/SendEmails');
$response = json_decode($response);
echo ($response);

But this one does nothing - Not even an error on how to go more into the problem.

I simpley need a php script to make the POST request to webapi who gets 1 param(String) and return An ok answer or Error,

6
  • 1
    you can do like this stackoverflow.com/questions/5647461/… Commented Apr 25, 2015 at 19:36
  • This works, thanks alot. How can i upvote or put a answer for your comment? Commented Apr 25, 2015 at 20:55
  • I glade it worked. When you hover a comment you will see an arrow point up on the left. You may click on it when a comment is useful. Commented Apr 26, 2015 at 2:02
  • It seems that if you only have 1 star you cant do that, sorry. Commented Apr 27, 2015 at 10:58
  • Grammer & english correction Commented Apr 28, 2015 at 9:59

1 Answer 1

3

After Maalls answer from this post How do I send a POST request with PHP?

The answer was really simple and the code was the following :

$url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);

Thanks Maalls and dbau for the answer :).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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