简体   繁体   中英

PHP/ASP.NET MVC Web-Api Somebody Know How To Use PHP To Call WEBAPI With POST

Please help me to tell me how to use the PHP to call asp.net web api ,thanks. sorry I didn't use PHP before , so I just want to use very simple code to call server side of the WebAPI .
Thank you for your patience if you have read my question to the end.

PS: I have done this issue and sharing how to handle this as following

How do I install CURL on Windows?

this url could be dealed with my problem , I will try to do it .thanks all

Steps

 1. to check your php.ini address , 2.Search or find the following : ';extension=php_curl.dll' 3.Uncomment this by removing the semi-colon ';' before it 4.Save and Close PHP.ini 5.Restart Apache 

reference address for all http://www.tomjepson.co.uk/enabling-curl-in-php-php-ini-wamp-xamp-ubuntu/

Normally the call is made through curl extension ( I advise you to take some time to read about it ) . Here is a function I use to make curl requests (in json):

function make_curl_request()
  {
          $data = []; // data to send 
          $data_string = json_encode($data);
          $ch = curl_init("http://localhost:8000/function");
          curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
          curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_HTTPHEADER, array(
              'Content-Type: application/json',
              'Content-Length: ' . strlen($data_string))
          );
          curl_setopt($ch, CURLOPT_TIMEOUT, 5);
          curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

          //execute post
          $result = curl_exec($ch);


          curl_close($ch);
          $result = json_decode($result,true);
          print_r($result);

  }

You need to use curl for that:

$curl = curl_init('http://myapi.com/');
$post_data = array(
    'post_var_1' => 'foo',
    'post_var_1' => 'bar'
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$curl_response = curl_exec($curl);
curl_close($curl);
if ($curl_response === false) {
    // handle api call error here ...
} else {
    // get your results from $curl_response here
}

How do I install cURL on Windows?

this url could be dealed with my problem , I will try to do it .thanks all

Steps

1.<?php echo phpinfo(); ?>  To check your php.ini address ,      
2.Search or find the following : ‘;extension=php_curl.dll’    
3.Uncomment this by removing the semi-colon ‘;’ before it    
4.Save and Close PHP.ini    
5.Restart Apache    

reference address for all
http://www.tomjepson.co.uk/enabling-curl-in-php-php-ini-wamp-xamp-ubuntu/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM