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/

4
  • If you don't know PHP at all, then hire someone to do it. You atleast need php fundamentals to make an API call. Commented May 18, 2015 at 9:43
  • thanks Daan , I just want to show how to across platfrom between PHP and Web Api, Commented May 18, 2015 at 9: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." Commented Dec 9, 2015 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 . Commented Dec 14, 2015 at 2:34

3 Answers 3

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

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

6 Comments

thanks thegentletrainer, but i've got "Call to undefined function curl_init()" , how can i deal with this problem,
you need to make sure that the curl extension is enabled in your server
create a php file with <?php echo phpinfo(); ?> 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
thankyou , but how to do , please help me to give my any resource to handle this issue ,thanks
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
|
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
}

6 Comments

thanks vard , but i've got "Call to undefined function curl_init()" , how can i deal with this problem,
You probably don't have curl on your server then. Do you try it on your development server or on your production server?
I have tried it on my server, the feedback it "Call to undefined function curl_init()",thanks vard
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.
sorry sir , it isn't found on the browser with "php echo phpinfo()"
|
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/

3 Comments

Thanks All , next time I will follow your advance to comment,thanks again ,and good luck .
Did this solve your problem? @Readers: if it solved his problem then it's a reasonable answer.
Dear All , yes this solution can be deat with this question , Thanks all

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.