1

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/

Willie Cheng
  • 7,679
  • 13
  • 55
  • 68
  • If you don't know PHP at all, then hire someone to do it. You atleast need php fundamentals to make an API call. – Daan May 18 '15 at 09:43
  • thanks Daan , I just want to show how to across platfrom between PHP and Web Api, – Willie Cheng May 18 '15 at 09:57
  • Not your fault willie. I think there are some folks that would rather downvote than simply answer a question. I am interested in the same thing as I am a C# developer that is now working on a php project. I don't know php but I have been developing software for 17 years. I don't need to be told to hire a php developer. As a response, that provides zero value. I just need someone with php experience to point me in the right direction. I thought that was the void sites like SO were supposed to fill. Yeah yeah, I know the question posting "rules." – chad Dec 09 '15 at 13:48
  • Hi @chad ,thanks you for your feedback, I am totally agreement with your comment, maybe next time we can help each other to deal with PHP problems, hopefully this is a good solutions too us . – Willie Cheng Dec 14 '15 at 02:34

3 Answers3

1

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);

  }
arakibi
  • 441
  • 7
  • 24
  • thanks thegentletrainer, but i've got "Call to undefined function curl_init()" , how can i deal with this problem, – Willie Cheng May 18 '15 at 10:03
  • you need to make sure that the curl extension is enabled in your server – arakibi May 18 '15 at 10:04
  • create a php file with inside it. call it in your browser and with ctrl+f search for "curl" .. and then see if it's enabled or not . I am sure it's not enabled ... and then you have to enabled ( just google it ) good luck – arakibi May 18 '15 at 10:06
  • thankyou , but how to do , please help me to give my any resource to handle this issue ,thanks – Willie Cheng May 18 '15 at 10:06
  • Hi thegentletrainer,, you are right ,I didn't find it out in the browser ,I try to handle this problem , will you mind give me more information ,thanks – Willie Cheng May 18 '15 at 10:23
  • http://www.walkswithme.net/enable-curl-library-in-xampp-localhost this mighit help you if you're using xampp . Otherwise , just google it . There are thousands of articles / tutorials – arakibi May 18 '15 at 10:56
1

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
}
vard
  • 4,057
  • 2
  • 26
  • 46
  • thanks vard , but i've got "Call to undefined function curl_init()" , how can i deal with this problem, – Willie Cheng May 18 '15 at 10:03
  • You probably don't have curl on your server then. Do you try it on your development server or on your production server? – vard May 18 '15 at 10:07
  • I have tried it on my server, the feedback it "Call to undefined function curl_init()",thanks vard – Willie Cheng May 18 '15 at 10:12
  • Is this a dedicated server that you can administrate, or a shared hosting ? If it's a shared hosting you'll probably not be able to get curl. Could you check in your phpinfo if `pecl_http` is available ? It's an other php extension you could use to achieve what you want to do. – vard May 18 '15 at 10:19
  • sorry sir , it isn't found on the browser with "php echo phpinfo()" – Willie Cheng May 18 '15 at 10:41
  • All right. For your information, for doing a phpinfo you need to put this (without `echo`) in your phpinfo.php file : `` – vard May 18 '15 at 12:07
1

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/

Community
  • 1
  • 1
Willie Cheng
  • 7,679
  • 13
  • 55
  • 68