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);
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:
http://curl.haxx.se/docs/manpage.html
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:
Convert command line cURL to PHP cURL
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:
How to post http request using digest authentication with libcurl
Related
I have locked my development-environment with a .htaccess-password.
While I'm now working on a script that uses a cURL-request to that htaccess-protected-folder, it doesn't work. When I delete the htaccess-protection it works fine.
Is there a way to block UserAgents, like GoogleBot and other human requests, but allow cURL ?
You can define the HTTP Auth username and password like this:
curl -u username:password http://...
This way you don't have to disable the HTTP Auth while accessing it from a browser but can access it from your script.
EDIT: If working with the PHP CURL object you can also define it as such:
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
I'm working on a PHP script that has to connect to an REST API. The provider of the API, suggested to use cURL. They gave me an example of how to use it in the command line:
curl -D- -u "user:password" -X GET -H "Content-Type: application/json" http://example.com/api/searchFunction?jql=assignee=user1
The PHP script is the following:
<?php
$defaults = array(
CURLOPT_HEADER => true,
CURLOPT_URL => 'http://example.com/api/searchFunction?jql=assignee=user1',
CURLOPT_USERPWD => "user:password",
CURLOPT_HTTPAUTH => 'CURLAUTH_BASIC'
);
$ch = curl_init();
curl_setopt_array($ch, ($defaults));
echo "cURL output: ".curl_exec($ch);
curl_close($ch);
?>
As you can imagine, the command line version works fine, but in the PHP version I got the following error:
Field 'assignee' does not exist or this field cannot be viewed by anonymous users.
That suggests that the user login validation doesn't works. However, the user and password are correct.
I was looking for already answered posts of cURL parameters equivalents between the command line version and the PHP version but couldn't find the correct parameters for the PHP version.
You haven't fully replicated your cURL command yet.
For starters, you've never set the Content-Type: application/json header option. You need to set that using the CURLOPT_HTTPHEADER option.
Secondly, command line cURL and PHP's cURL use different User-Agent values.
Consider enabling the command line cURL's verbose option so you can see all the information it's sending, then replicate it PHP.
UPDATED THANKS TO ANSWERS:
Can someone point out the difference between:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_root);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "xml"); // tried http_build_query also
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Added this, still no good
return curl_exec($ch); // returns false
and:
$curl = "curl -X POST -d 'xml' {$api_root}";
return `$curl`; // returns expected xml from server
AND/OR
More generally, are there any good breakdowns out there for conversion/reference between php's libcurl default values/headers and those of curl on the command line?
I know this is almost a dupe of curl CLI to curl PHP and CLI CURL -> PHP CURL but I'm hoping for something more definitive.
When you use backticks then PHP invokes a shell. This can be dangerous, especially when you include variables in the command. If someone has a way to influence the value of $api_root they would be able to invoke any command on your system.
Using the API is much safer and probably faster as well as the curl libraries are loaded into PHP.
As for why it's not working it seems others have answered that question :)
curl_exec returns true or false by default. You need to specify CURLOPT_RETURNTRANSFER:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Since curl_exec is returning false (not NULL as indicated in the original question), try using curl_error() to determine why it's returning false.
TFM (read it): curl_exec(), curl_setopt()
Edit for posterity's sake:
The OP discovered that an SSL issue was the hindrance. The both libcurl (as called through PHP) and the curl command-line do SSL peer verification for every transaction, unless the user explicitly disables it.
The likely scenario is that the shell environment is using a different CA bundle than PHP's libcurl implementation. To remedy this, set CURLOPT_CAINFO to be the same as the shell's CURL_CA_BUNDLE environment variable and then peer verification should work.
#OP: I'd be curious to know if the above suggestion is confirmed working in your case, or if there is something else different with the SSL configuration.
in your php example you are missing
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
from php manual:
curl_exec
Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
Add this line:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
To match the CLI version:
$curl = "curl -X POST -d 'xml' {$api_root}";
return `$curl`; // returns expected xml from server
I also needed:
// Thanks to all the answers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// This appears to default false on CLI, true in libcurl
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
How to set the PHP_AUTH_PW and PHP_AUTH_USER parameters in php curl.
At the server end its checking for:
if(!isset($_SERVER['PHP_AUTH_PW']))
{
print "Authorization error"
}
Any help would be appreciated
Thanks
It is called basic-auth, and works with most browsers including curl on command line:
curl --user name:password http://www.example.com
and in PHP you set two options on your curl connection ($curl_conn):
curl_setopt($curl_conn, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl_conn, CURLOPT_USERPWD, 'username:password');
Hello
I am working with a legacy system where an ASP.NET application posts an XML file to a server via curl.exe (this url to send is configurable by a .config file).
Now due to legacy system limitations, I need curl post this XML to my ubuntu server by changing the said .congfig file, modify the received XML as I need and finally curl post it to the real server.
How can this be done ? My guess is a php or a python script running under apache2 server, listening posts. Once received the xml file, do the required modifications on the file and post to the real curl server.
Via php or python, how can this be done ?
Since ASP.NET application is posting XML, you simply need to handle a normal POST request, modify XML to match your requirement and post it using cURL to the real cURL server. In PHP, it would look something like this (more or less meta code, error checking and additional logic is needed):
$xml = $_POST['xml'];
// do something with posted XML
.....
// post it to the "real" cURL server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('xml' => $xml));
$result = curl_exec($ch);
curl_close($ch);
That's about it, check cURL documentation and use what is necessary for POST to work with your server, and your are all good.