X

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.

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:

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.

Liked this post? Empower your friends by sharing it!
Categories: ASP.NET

View Comments (52)

  • I'm not sure exactly why but this web site is loading very slow for me.
    Is anyone else having this issue or is it a problem on my end?

    I'll check back later on and see if the
    problem still exists.

  • 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.

  • Nice respond in return of this issue with solid arguments and describing
    all regarding that.

  • 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!

  • 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

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

  • 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

  • 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!!

  • 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.

  • 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!

  • It's nearly impossible to find knowledgeable people on this topic, however, you sound
    like you know what you're talking about! Thanks

  • 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

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

  • 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

  • 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

  • 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.

  • I’m not that much of a internet reader to be honest but your sites really nice, keep
    it up! I'll go ahead and bookmark your website to come back down the road.
    Many thanks

  • 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.

  • 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!

  • 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.

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

  • 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!!

  • 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.

  • 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

  • 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!

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

  • 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.

  • Hi there, yes this paragraph is actually fastidious and I have learned lot of things from it regarding blogging.
    thanks.

  • Now I am ready to do my breakfast, once having my breakfast
    coming again to read other news.

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

  • 823902 294418This will likely be a fantastic internet web site, will you be involved in performing an interview regarding how you developed it? If so e-mail me! 905491

  • I constantly spent my half an hour to read this weblog's content everyday along with a cup of coffee.

  • Hi there it's me, I am also visiting this web page
    on a regular basis, this website is really fastidious
    and the visitors are really sharing good thoughts.

  • Basic Game Info • May 22, 2025: Games Global and BetMGM have partnered to launch a new slot game, Gold Blitz Ultimate. This game is the third in the Gold Blitz series. Gold Blitz Ultimate initially will be exclusive to BetMGM Casino in New Jersey, Pennsylvania and Michigan. It features six reels, 6,400 paylines and has an average RTP rate of 96%. • May 22, 2025: Games Global and BetMGM have partnered to launch a new slot game, Gold Blitz Ultimate. This game is the third in the Gold Blitz series. Gold Blitz Ultimate initially will be exclusive to BetMGM Casino in New Jersey, Pennsylvania and Michigan. It features six reels, 6,400 paylines and has an average RTP rate of 96%. The king of the prairies of North America returns Surebet247 frequently collaborates with top-tier software providers to bring you exclusive game variants you won’t find elsewhere. From branded slots to bespoke table layouts, these games add a layer of uniqueness that keeps our library fresh and compelling. We’re constantly updating our portfolio, so keep an eye out for upcoming releases and exclusive feature rollouts.
    https://www.1001crochet.com/ideas/reward-multiplier-benchmarks-in-football-x-best-and-worst-outcomes/
    Moreover, this bonus mode can be retriggered if three or more scatter icons land, granting the player an additional three spins. There is no limit to the number of times this mode can be retriggered. The first noticeable difference is the drastically enlarged, albeit technically unremarkable, Megaways setup. Lashed wooden beams prop up 6 main reels holding 2-7 symbols each as well as an up top horisontal reel holding 4 symbols. At full whack, a massive 200,704 ways to win are in play, compared to 4,096 ways of the original. The rest of the view, what you can make out at least, is essentially the same rocks and canyons as before. An outdoorsy soundtrack completes the scene, and fans of the first Buffalo King won't be scared away by anything too radical, aesthetically, at least. White Rabbit boasts wilds, bonus wilds, free spins, feature drops and scatters. Look for the caterpillar because the little critter brings multiple bonus wild symbols.

  • I used to be suggested this website via my cousin. I am no longer positive
    whether or not this publish is written by him
    as no one else recognise such targeted approximately my difficulty.

    You are incredible! Thank you!

  • It's actually a nice and useful piece of information. I am glad that you shared this helpful information with us.

    Please keep us up to date like this. Thanks for sharing.

  • Currently it appears like Wordpress is the best blogging platform out there right now.
    (from what I've read) Is that what you are using on your
    blog?

  • My developer is trying to convince me to move to .net from PHP.
    I have always disliked the idea because of the expenses.
    But he's tryiong none the less. I've been using Movable-type on several websites for about
    a year and am concerned about switching to another
    platform. I have heard great things about blogengine.net.
    Is there a way I can import all my wordpress content into it?
    Any kind of help would be greatly appreciated!

  • Great blog you have here.. It's hard to find high-quality writing like yours nowadays.
    I really appreciate people like you! Take care!!

  • I loved as much as you'll receive carried out right here.
    The sketch is attractive, your authored subject matter
    stylish. nonetheless, you command get bought an impatience over that you
    wish be delivering the following. unwell unquestionably come more formerly
    again since exactly the same nearly very often inside
    case you shield this hike.

  • This is a good tip particularly to those new to the blogosphere.
    Short but very precise info… Many thanks for sharing this one.
    A must read article!

Related Post