bart2puck
bart2puck

Reputation: 2522

sending rest request via php

I am trying to send a REST request. The example I have been given by the system docs is this:

 $ curl --digest -u admin:<passwd> http://1.2.3.4/r/users/12345/calls/recent

{"data": [
  {"state_msg": "Finished",
   "code": 200,
   "dst_codecs": "PCMU,PCMA,iLBC,telephone-event",
   "src_codecs": "PCMU,PCMA,telephone-event,iLBC",
   "pid": 1250018007,
   "url": "\/r\/users\/12345\/calls\/1250018007:16739",
   [...]
  }
  [...]
]}

what is this example trying to tell me? what is the data information there? Is that what i need to send. If so, how would i send it? I have read this post: Call a REST API in PHP but I am still unsure of how to structure my call. would it be something like this?

 $data = array('state_msg' => 'state_msg','code'=>'200'.....);

 curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
 curl_setopt($curl, CURLOPT_USERPWD, "admin:<password>");
 curl_setopt($curl, CURLOPT_URL, "http://1.2.3.4/r/users/12345/calls/recent");
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

Upvotes: 0

Views: 818

Answers (1)

hakre
hakre

Reputation: 197765

I start with the beginning of the example:

$ curl

The $ sign denotes a unix shell prompt with standard user privileges.

Then a space separates the command which is curl here.

Each command has (normally) a manual page, you get it with the man command:

$ man curl

That should explain all the rest to you, as those man-pages explain all of the commands switches and options.

If you don't have such a shell prompt at hand and you do not like to consider installing one, many commands have their man pages as well in the internet. Here for curl:

After you've understood what that concrete command does, you just look-up the related options in the PHP manual on the curl_setopt page. How this works is demonstrated in the following example:


Example:

$ curl --digest -u admin:<passwd> http://1.2.3.4/r/users/12345/calls/recent
       ########

This switch relates to the CURLAUTH_DIGEST value of the CURLOPT_HTTPAUTH setting.

$handle = curl_init($url);
curl_setopt_array($handle, [
    ...
    CURLOPT_HTTPAUTH => CURLAUTH_DIGEST, // --digest
    ...
]);

Compare with the Curl C-API which is just wrapped by PHP:

Upvotes: 1

Related Questions