Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a Web Service from PHP

How can i call a web service from php

like image 623
Warrior Avatar asked May 06 '26 05:05

Warrior


1 Answers

Use the curl function:

http://php.net/manual/en/book.curl.php

Assuming you are using a GET request to connect to a RESTful API:

$url = "http://the-api-you-want/?the-args=your-args&another-arg=another-arg"; 
$ch = curl_init(); // start CURL
curl_setopt($ch, CURLOPT_URL, $url); // set your URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // get the response as a variable
$json = curl_exec($ch); // connect and get your JSON response 
curl_close($ch);

You can then use PHP's json_decode on the response if that is what you want to do.

Another option would be using Drupal's drupal_http_request function http://api.drupal.org/api/function/drupal_http_request/6

like image 122
Finbarr Avatar answered May 09 '26 00:05

Finbarr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!