Calling a web service from PHP

How can I call a web service from php

+3
source share
3 answers

Use function curl:

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

Then you can use PHP json_decodeto answer if that is what you want to do.

Another option would be to use the Drupal function drupal_http_request http://api.drupal.org/api/function/drupal_http_request/6

+5
source

I would recommend you to be more specific in your Question. What type of web service do you mean?

Rest Webservices, Zend_Rest_Client, Zend Framework. , Zend Framework SOAP-.

0

Use curl or the Zend_Http_Client library from the Zend Framework (you don't need the whole Zend Framework to use the library). If the service you are calling sends a JSON response, you will have to parse it in PHP using json_decode .

0
source

Source: https://habr.com/ru/post/1742650/


All Articles