I am working on a PHP webservice that will be making REST calls to an application API. My webservice is built on a LAMP server running ubuntu and the following command is working perfect from command line:
curl -k -u username -H "Content-type: application/xml" -X PUT -d #request.xml https://server/path/
However, when I try to build the same request in PHP I am getting the following response from the REST server, indicating that the XML is missing:
HTTP400Premature end of file.
Here is the code of my PHP request:
$ch2 = curl_init($service_url);
$headers = array('Content-Type: application/xml', 'Accept: application/xml');
curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch2, CURLOPT_PUT, 1);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch2, CURLOPT_POSTFIELDS, $xmlRequest);
curl_setopt($ch2, CURLOPT_USERPWD, 'user:password');
$curl_response = curl_exec($ch2);
Everything I have read indicates the CURLOPT_POSTFIELDS is the place to do this, but I thought this was for adding "?" variables to the actual URL. Anyway, any help would be appreciated.
According to the manual, if you use CURLOPT_PUT you should also provide CURLOPT_INFILE and CURLOPT_INFILESIZE.
Maybe instead of
curl_setopt($ch2, CURLOPT_POSTFIELDS, $xmlRequest);
try
curl_setopt($ch2, CURLOPT_INFILE, fopen('request.xml', 'r'));
curl_setopt($ch2, CURLOPT_INFILESIZE, filesize('request.xml'));
Related
The documentation says like this:
curl —X POST -c cookies.txt —d "login=demo&password=demo42" https://www.myadcash.com/console/login_proxy.php
Output will be:
{"token":"6333531373034343433623663646836383165693937383167373264323334663"}
My current code
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("login"=>"demo","password"=>"demo42"));
curl_setopt($ch,CURLOPT_URL,"https://www.myadcash.com/console/login_proxy.php");
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . 'cookies.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
No JSON response is showing. Although the text file is saving. Please help me to find right direction. Also if there is any error in the code, please let me know.
-d "login=demo&password=demo42" means data field, thats why pass as post fields not headers.
Therefore these two lines in your curl
curl_setopt($ch, CURLOPT_POST, 1); //Optional
curl_setopt($ch, CURLOPT_POSTFIELDS, "login=demo&password=demo42");
and you must get output.
On the basis of documentation, credentials must be post fields not header that's why no need to put login and password on header.
You don't need
curl_setopt($ch, CURLOPT_HTTPHEADER, array("login"=>"demo","password"=>"demo42"));
Recently I created my owncloud server and I need to be able to upload a file from a php form which transfer an file from my pc to my owncloud server. So I tried to use Curl, like this :
<?php
$url = "5.25.9.14/remote.php/webdav/plus.png";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // -X PUT
curl_setopt($ch, CURLOPT_USERPWD, "root:root"); // --user
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'img/plus.png' => '#'.realpath('img/plus.png')
)
);
$output = curl_exec($ch);
curl_close($ch);
?>
I have been inspire by this post and this command :
curl -X PUT "http://server.com/owncloud/remote.php/webdav/file.zip" --data-binary #"/Users/Root/Downloads/file.zip"
The command line, he's working but not my php. I succeed to upload the file but the file is corrupted and I don't know why :/. Maybe I miss the MIME type ? Is it enough to get a corrupted file ?
Do you see where I am wrong ?
Best regards, Zed13
Edit : When I make an file of my uploaded file, it's of type data and not png, strange...
I was having an issue with an upload to owncloud as well. Had the same symptoms, the command line curl works, but not the PHP curl call.
Thanks to your post I was able to get it working. Here is what works for me
// upload backup
$file_path_str = '/tmp/' . date('Ymd') . '.tar.gz';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://server/remote.php/webdav/backups/' . basename($file_path_str));
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_PUT, 1);
$fh_res = fopen($file_path_str, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary
$curl_response_res = curl_exec ($ch);
fclose($fh_res);
The differences are:
CURLOPT_PUT instead of CURLOPT_CUSTOMREQUEST
CURLOPT_INFILE and CURLOPT_INFILESIZE instead of CURLOPT_POSTFIELDS
Thanks for your help.
//
I am trying to convert the following Curl command:
curl --digest --user "username:password" --verbose --url "http://127.0.0.1/ws?graph-uri=http://localhost/dataset/import/" -X POST -T /data/datasets/foo.n3
Here is the code that appears to be ok, but that doesn't work:
$args = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/ws?graph-uri=http://localhost/dataset/import/');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$args['graph-uri'] = curl_file_create('/data/datasets/foo.n3');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$result = curl_exec($ch);
It seems that the file is actually not uploaded with the PHP code, but everywhere I look the usage of curl_file_create() seems good.
Try this previous answer, substituting graph-uri for file_contents. Also, you are setting "graph-uri" as both GET and POST params, which could collide, and the graph-uri GET param is not URL encoded, which could behave differently in the shell vs. PHP. So you might try the following:
http://127.0.0.1/ws?graph-uri=http%3A%2F%2Flocalhost%2Fdataset%2Fimport%2F
I need to delete opencast matterhorn recording via REST api which they already provided. (just for the information, no need to worry about matterhorn)
I need to develop simple PHP code to DELETE some entries via given REST API. I have tested with curl command line it is working fine, but I can't convert that into working PHP code.
working curl command :
curl --digest -X "DELETE" -u matterhorn_system_account:CHANGE_ME -H "X-Requested-Auth: Digest" -H "X-Opencast-Matterhorn-Authorization: true" url/search/xxxx
not working PHP command :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'url/search/xxxx');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, 'matterhorn_system_account:CHANGE_ME');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-Auth: Digest"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Opencast-Matterhorn-Authorization: true"));
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
result $httpCode is 302, means it's not working.
Any idea where I went wrong.
Thanks in advance
You need to combine these two so you aren't overwriting yourself:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-Auth: Digest"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Opencast-Matterhorn-Authorization: true"));
So that should be
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-Auth: Digest",
"X-Opencast-Matterhorn-Authorization: true"));
And it probably would also help to follow redirects with
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
I have the following code:
$body = "<SOAP-ENV:Envelope>".$data."</SOAP-ENV:Envelope>";
$ch = curl_init($FD_Add);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "WS******._.1:********");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSLCERT, DIR_ROOT."ABCDEFG/Certs/WS******._.1.pem");
curl_setopt($ch, CURLOPT_SSLKEY, DIR_ROOT."ABCDEFG/Certs/WS******._.1.key");
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, "ckp_***********");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if($test_env){echo 'The result of your request is:<RESULT>'.$result.'</RESULT>br/>' ;}
curl_close($ch);
The response is an error 401 HTTP requires authentication. I have verified the user ID and Password to be correct. What I am wondering first is if the version of PHP could cause this error if it does not support CULROPT_AUTH as I am running PHP~v. 3.4 which from what I read does not support this option.
Anybody know if this could be the trouble?
You could probably fake it by issuing the basic auth header yourself:
$auth_header = "Authorization: Basic " . base64_encode("$user:$pass");
curl_setopt($ch, CURLOPT_HTTP_HEADER, $auth_header);
but that's only available since Curl 7.10.3 and later. Your stone-age PHP is most likely using an equally stone-age CURL.
Really... PHP 3.4 is the equivalent of driving around in a car bought from the Flintstones. You should upgrade to something more modern, like a Model-T... those horseless carriages are pretty impressive.