Converting a Curl command into PHP Curl code (file transfer & custom command) - php

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

Related

PHP shell_exec get file

Is there any way to get file contents of external webiste, say https://www.example.com/.
For eg. Using Python from shell_exec. I tried python but got blank.
Not sure why you want to use shell_exec but
you can use
$data = file_get_contents('https://www.example.com/');
or
$url = 'https://www.example.com/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
if you want to maintain the session you could use curl with CURLOPT_COOKIEFILE
eg:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, 'https://www.example.com/');
$data = curl_exec($ch);
or if you really want to use the shell_exec the use follwing
$data = shell_exec('curl -I https://www.example.com/');
echo "<pre>$data</pre>";
however for above to work you have to have curl installed check following for instillation of curl
https://curl.se/
hope this works

PHP CURL PUT Equivalent to Command Line

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

PHP curl only sending POST data once

I have the following PHP code:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://10.0.0.99/api/login");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "somethingsomething");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/plain',
'Content-Length: 18')
);
$cookie = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "https://10.0.0.99/api/getcmds");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIE, "cookie=".$cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/plain',
'Content-Length: 0')
);
$response = curl_exec($ch);
$json = json_decode($response, true);
curl_setopt($ch, CURLOPT_URL, "https://10.0.0.99/api/cmd/" . $json["value"]);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIE, "cookie=".$cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "on");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/plain',
'Content-Length: 2')
);
curl_exec($ch);
curl_close($ch);
The first call to curl_exec posts the string as expected and returns the response. Second call also works as expected.
The third call however doesn't post the string "on" to the server. It correctly gets the returned data (which, without the "on" is an error), but the server never receives the posted data.
The code itself is running on PHP 5.5.30 in the command line using the -f flag. Curl Verbose mode is showing that the Content-Length is beeing set correctly, however it doesn't show anything about posted data. The rest of the post seems to be correct, it's just that no data arrives.
(PS: This is not supposed to be production code, just something I'm playing around with, so please no comments about code quality/security.)
You can try closing the connection and then starting again for a new call/request. I personally do this thing.
From php.net docs on cURL: PHP: curl_setop
As for CURL_POSTFIELDS:
This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value.
In your case, you set an empty variable $_POST['on']. If you want it to have some particular value:
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "on=value");

No Response getting from Curl Request to https server

I am creating a PHP curl request to server and expecting some string in response but no response generating , What may be the reason for that?
Below is the Curl code for the same:
$fields = array("username"=> 'xxx',"domain"=> 'xxx',"password" => 'xxx');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://host:8090/add-user");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $usr.':'.$pass);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if(!$result=curl_exec($ch)){
trigger_error(curl_error($ch));
}
curl_close($ch);
var_dump($result);
Below is equivalent command line request for the same:
curl --data 'username=xxx&domain=xxx&password=xxx' https://host:8090/add-user -k -v -u 'usr:pass'
What i am doing Wrong Here?
You should first try to use curl_setopt($ch, CURLOPT_VERBOSE, $file_handler); where $file_handler = fopen('somefile.log', 'a'). Thus you will see what exactly cURL does, and where it fails.

convert curl line command in php code

can anyone help me to convert that curl comand line in a php curl code ? plz
curl -v --data "WSCommunityStringRW?2=1200ve50set&Submit=Submit" http://xxxxx/123 -u "admin:a1s2d3" --anyauth
Yes.I did something but ... not work :(
$api_url = 'xxx/123';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, "admin:a1s2d3");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'WSCommunityStringRW?2' => '1200ve50set',
'Submit' => 'Submit'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_close($ch);
Have you taken a look at the cURL pages in the PHP manual? It should contain everything you need for this.
Edit
In the code you've provided, curl_exec() is missing. If it doesn't work after calling curl_exec(), try looking at the output of curl_error().

Categories