Consuming ASP.NET Web API services from PHP script

I introduced ASP.NET Web API in some of my previous posts. Although Web API is easy to use in ASP.NET web applications you can use Web API also from other platforms. This post shows you how to consume ASP.NET Web API from PHP scripts.

Here are my previous posts about Web API:

Although these posts cover content negotiation they give you some idea about how Web API works.

Test application

On Web API side I use the same sample application as in previous Web API posts – very primitive web application to manage contacts.

ASP.NET Web API test application

Listing contacts

On the other machine I will run the following PHP script that works against my Web API application:

<?php   
// request list of contacts from Web API 
$json = 
file_get_contents(‘http://vs2010dev:3613/api/contacts/’);

// deserialize data from JSON 
$contacts = json_decode($json); 
?> 

<html> 
<head> 
    <meta http-equiv=
"Content-Type" content="text/html; charset=utf-8"
 /> 
</head> 
<body> 
    <table> 
    
<?php
  
    
foreach($contacts as $contact) 
    { 
        ?> 
        <tr> 
            <td valign=
"top"> 
                <?php 
echo $contact->FirstName ?> 
            </td> 
            <td valign=
"top"> 
                <?php 
echo $contact->LastName ?> 
            </td> 
            <td valign=
"middle"
> 
                <form method=
"POST"
> 
                    <input type=
"hidden" name="id"  
                        
value=
"<?php echo $contact-/>Id ?>"
 /> 
                    <input type=
"submit" name="cmd"  
                        
value=
"Delete"
/> 
                </form> 
            </td> 
        </tr> 
        
<?php 
    } 
    ?> 
    </table> 
</body> 
</html>

Notice how easy it is to handle JSON data in PHP! My PHP script produces the following output:

PHP form showing data from ASP.NET Web API

Looks like data is here as it should be.

Deleting contacts

Now let’s write code to delete contacts. Add this block of code before any other code in PHP script.

if(@$_POST[‘cmd’] == ‘Delete’) 
{
 
    $errno = 0
; 
    
$errstr = 
; 
    
$id = @$_POST[‘id’]
; 
     
    
$params = 
array(‘http’ => array( 
              ‘method’ => ‘DELETE’
, 
              
‘content’ => "" 
            ));

    $url = ‘http://vs2010dev:3613/api/contacts/’.$id; 
    
$ctx = 
stream_context_create($params); 
    
$fp = 
fopen($url, ‘rb’, false, $ctx); 
      
if (!$fp) { 
        $res = false
; 
      
} else { 
        $res = stream_get_contents($fp)
; 
      
} 
    
fclose
($fp);

    header(‘Location: /json.php’); 
    
exit
; 
}

Again simple code. If we write also insert and update methods we may want to bundle those operations to single class.

Conclusion

ASP.NET Web API is not only ASP.NET fun. It is available also for all other platforms. In this posting we wrote simple PHP client that is able to communicate with our Web API application. We wrote only some simple code, nothing complex. Same way we can use also platforms like Java, PERL and Ruby.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    37 thoughts on “Consuming ASP.NET Web API services from PHP script

    • April 10, 2025 at 10:54 am
      Permalink

      Thanks foor every other fantastic article. Where else
      may just anyone get that type of information in such an ideal means of writing?
      I’ve a presentation next week, and I’m at the search for such information.

    • April 11, 2025 at 12:51 pm
      Permalink

      Hi there, just became alert to your blog through Google, and found that it is truly
      informative. I’m gonna watch out for brussels. I will be grateful if you continue this in future.
      Lots of people will be benefited from your writing. Cheers!

    • April 13, 2025 at 12:59 pm
      Permalink

      Hi! This is kind of off topic but I need some guidance from an established blog.
      Is it hard to set up your own blog? I’m not very
      techincal but I can figure things out pretty quick. I’m thinking about creating my own but I’m not sure where to begin. Do you have any ideas or suggestions?
      Appreciate it

    • April 15, 2025 at 8:52 am
      Permalink

      Your mode of explaining everything in this post is in fact pleasant, all be capable of simply be aware of it, Thanks
      a lot.

    • April 18, 2025 at 12:48 am
      Permalink

      jili apps login – best slot online іn thе
      Philippines
      Ƅetter tһan fachai slot аnd pg slot
      free Spins 3000+ Free Demo Slot

      searching jiliasia8.ph ⲟn Google or Bing to get free coins for play

      jili apps

    • April 18, 2025 at 3:12 am
      Permalink

      An intriguing discussion is definitely worth comment.
      There’s no doubt that that you should write more on this
      issue, it might not be a taboo matter but usually folks
      don’t discuss these topics. To the next! Many thanks!!

    • April 19, 2025 at 5:56 pm
      Permalink

      In the burgeoning marketplace of cannabidiol (Pure earth CBD Review)
      products, Tricolla Farms has emerged as a noteworthy marque known for its committedness to
      quality, transparency, and client expiation.

    • April 22, 2025 at 10:00 am
      Permalink

      Good day! Do you know if they make any plugins to help with
      Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords
      but I’m not seeing very good results. If you know of any please share.

      Thanks!

    • April 30, 2025 at 7:09 pm
      Permalink

      In the world of health and fitness, supplements have become an integral part
      of many individuals’ routines, helping them achieve their performance and wellness goals.

      Feel free to visit my webpage: Vigor Pump Supplement

    • May 4, 2025 at 9:08 am
      Permalink

      I am regular visitor, how are you everybody? This piece of writing posted at this web site
      is actually pleasant.

    • May 6, 2025 at 5:05 pm
      Permalink

      PG SOFT Gaming – best slot online in thе Philippines
      Ьetter than jili slot and bingo ρlus
      free Spins 3000+ Free Demo Slot

      searching pggaming.ⲟrg on Google օr Bing to ɡеt free coins foг play

      pg game

    • May 8, 2025 at 3:38 pm
      Permalink

      Sweet blog! I found it while browsing on Yahoo News.
      Do you have any tips on how to get listed in Yahoo News?
      I’ve been trying for a while but I never seem to get there!
      Thank you

    • May 9, 2025 at 4:41 am
      Permalink

      Excellent article. Keep writing such kind of info on your
      page. Im really impressed by your blog.
      Hi there, You’ve performed a fantastic job.
      I will definitely digg it and personally suggest to my friends.
      I am sure they’ll be benefited from this site.

    • May 16, 2025 at 10:24 am
      Permalink

      Hi to every one, for the reason that I am actually keen of
      reading this weblog’s post to be updated on a
      regular basis. It contains fastidious material.

    • May 18, 2025 at 10:52 am
      Permalink

      I like the helpful information you provide in your articles.
      I will bookmark your blog and check again here regularly.
      I am quite certain I’ll learn plenty of new stuff
      right here! Best of luck for the next!

    • May 23, 2025 at 12:20 am
      Permalink

      Hi there it’s me, I am also visiting this
      web page regularly, this website is genuinely fastidious and the people are in fact sharing
      pleasant thoughts.

    • May 23, 2025 at 7:42 am
      Permalink

      Thanks for finally writing about > Consuming ASP.NET Web API services from PHP script < Loved it!

    • May 27, 2025 at 3:12 am
      Permalink

      Hi, i think that i saw you visited my web site thus i came to “return the favor”.I’m attempting to find things to improve my website!I suppose its ok to use
      some of your ideas!!

    • May 28, 2025 at 6:59 pm
      Permalink

      Just wish to say your article is as amazing. The clarity in your submit is simply spectacular and i could suppose you’re knowledgeable
      in this subject. Fine with your permission let me to snatch your feed to
      keep up to date with coming near near post. Thanks a
      million and please continue the enjoyable work.

    • May 31, 2025 at 6:02 am
      Permalink

      Hawkplay login – Үоu ϲɑn Use GCash to play in anytime
      Free Spins 1000+ Free Demo Slot
      Openning Bonus 120% piso Ƅack
      Searching hawkplay casino on Google օr Bing to ցet free coins

      hawkplay login

    • June 7, 2025 at 12:37 am
      Permalink

      Thanks for your personal marvelous posting! I quite
      enjoyed reading it, you may be a great author.I
      will ensure that I bookmark your blog and will often come back in the
      foreseeable future. I want to encourage you to continue your great posts, have a nice day!

    • June 7, 2025 at 4:51 pm
      Permalink

      What’s up, the whole thing is going perfectly here and ofcourse every one
      is sharing data, that’s genuinely good, keep up writing.

    • June 10, 2025 at 4:14 am
      Permalink

      Good post. I learn something new and challenging on blogs I stumbleupon every day.
      It’s always interesting to read content from other writers and practice a
      little something from their web sites.

    • June 14, 2025 at 1:51 am
      Permalink

      These are truly wonderful ideas in about blogging. You have touched some
      good factors here. Any way keep up wrinting.

    Leave a Reply

    Your email address will not be published. Required fields are marked *