0

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);
Community
  • 1
  • 1
bart2puck
  • 2,432
  • 3
  • 27
  • 53
  • It is always something like that, to find out about the details, see my answer below it explains how it works. If you follow that you should be able to translate *any* curl commandline to PHP. If not, open a new question asking for the concrete part you're having a problem with (some are not so easy to translate, but most are 1:1 so very easy). – hakre Aug 29 '13 at 15:15
  • I appreciate the response. Being new to rest requests, I guess what I am trying to ask is: is that {"data"..... stuff part of the request or part of an example response? I obviously know you have no clue what system I'm working with, or what the docs intended to say, but is that structure of data common in REST? – bart2puck Aug 29 '13 at 16:02
  • Okay, I see. That is part of the response. The request method is default (GET). username and password should be clear. the HTTPAUTH is wrong in your code above, see my answer below it covers that part (which I think is the most important mistake you made and probably most hard to look-up). – hakre Aug 29 '13 at 16:06
  • I dunno which operating system you're using, but I suggest you to install curl on your system and just try the request your own. In any case when you're dealing with REST or just PHP, having curl on the machine is a life-saver because you can use it to debug many issues quite easily. - http://curl.haxx.se/download.html - I'd say it's also mean on those docs you should try if it works for you on the commandline as well. – hakre Aug 29 '13 at 16:08
  • yea, the command works fine on command line. I am trying to integrate it into a web services page for our customers. I think @hakre nailed it. it looks like im having some unrelated server issues. Thank you. – bart2puck Aug 29 '13 at 16:30
  • 1
    Okay, if you need to debug a curl request in PHP, you might find this Q&A helpful: [Php - Debugging Curl](http://stackoverflow.com/questions/3757071/php-debugging-curl/14436877#14436877) – hakre Aug 29 '13 at 16:32

1 Answers1

1

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:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836