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:
- How content negotiation works?
- ASP.NET Web API: Extending content negotiation with new formats
- Query string based content formatting
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.
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.
https://filedn.eu/lxtC6dFlc5qkLyWtpRp8Gdb/search-marketing-399/research/marketing-(79).html
There are many dressing options available for each price range.
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.
I couldn’t refrain from commenting. Perfectly written!
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.
KickStart Keto Weight loss
Keto has emerged as a popular weight loss supplement, promising rapid and sustainable fat burning through a ketogenic approach.
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!!
Pure Earth CBD Reviews Earth CBD is
a relatively new entrant in the burgeoning CBD market, promising users a range of
products boasting “clean, effective, and sustainable CBD solutions.
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.
KickStart Keto review Keto has emerged as a popular weight loss
supplement, generating significant buzz in the health and
wellness industry.
In Recent years, Cannabidiol (Activpure CBD Ingredients) has gained far-flung recognition for its expected curative benefits.
https://je-tall-marketing-542.tor1.digitaloceanspaces.com/research/je-tall-sf-marketing-(374).html
You may opt for a classic shade such as navy, or select a summery shade just like the Eliza j ruched waist jumpsuit .
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.