cURL: from PHP to BASH - php

I've never done any curl before so am in need of some help.
php:
<?php
$ch = curl_init();
$data = array(
'uptype'=>'file',
'file'=>'#'.$argv[1],
);
curl_setopt($ch, CURLOPT_URL, 'http://my_site_ex/up.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
curl_close($ch);
?>
how to make the same script in BASH?

I believe it's:
curl -F "uptype=file" -F "file=#$1" 'http://my_site_ex/up.php'
The -F uses multipart/form-data, which the PHP interface libcurl uses if you pass an array for CURLOPT_POSTFIELDS. Each -F is a separate field. libcurl reads the file you specify with #.

I belive its like so
data='-F "uptype=file" F "file=#$1"'
server="http://my_site_ex:8080/up.php"
opts="-v"
curl $server $opts $data
Im not 100% unfortunately but its something along these lines.

Related

How to correctly code Curl request in PHP with jsonrpc parameters?

I have a curl command that worls perfectly form the SSH shell, but cannot get it working with PHP Version 7.4.3.
I have tried several times using just about every example I can find on the Internet, all of them return a blank html page when run.
Here is the curl command (with user/pass and IP etc changed for obvious reasons);
curl \
--user someuser:password \
--data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getinfo", "params": [] }' \
-H 'content-type: text/plain;' \
http://195.86.111.23:8223/
Can someone please educate me on how to turn this into something PHP can use?
I am currently testing with this code, but get an error "{"result":null,"error":{"code":-32600,"message":"Params must be an array"},"id":"curltest"} "
$payload=array();
$payload['jsonrpc']='1.0';
$payload['id']='curltest';
$payload['method']='getinfo';
$payload['params']='[]';
$payload=json_encode($payload);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $pass);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res=curl_exec($ch);
echo $res;
I have also tried using an automated tool here - https://incarnate.github.io/curl-to-php/ which seems to convert my curl command to PHP okay, but the page is empty on loading the provided code.
$payload = ["jsonrpc" => "1.0", id => "curltest", "method" => "getinfo", "params" => [] ];
Simple solution!

PHP Curl Not working

I am new here and not a pro in php but need a small help. Actually I was given a code by my service prodvider to upload on my hosting. But when i uploaded that code on my hosting account it didnt work well and after contact the support I was told to use php curl code in some part to make it run. can anyone please help me with the issue.
Original Code:
<?php
$MobileNumber=substr($_REQUEST['VerifiedNumber'],-10);
$APIKey="<Your Dial2verify API Key Here>";
$SenderID="<6 ALPHABETIC CHARACTERS ONLY>";
$Message="<Your SMS Text Here>";
echo `curl -XPOST "http://host/SMS/SEND/$APIKey/$SenderID/$MobileNumber" -d "Msg=$Message"`;
?>
I don't know how to make the above code into curl code to make it work.
Thanks :)
The reason it did not work was that you where trying to call the command line version of cURL (and also doing the call wrong).
The best way is to use the php cURL module to make this call. Verify first that it is installed by creating an info.php file with the contents
<?php phpinfo();
If cURL is present on that page you are good to go.
Obviously I have not had the ability to test this code but something like this should work
$msisdn = substr($_REQUEST['VerifiedNumber'],-10);
$apiKey = "<Your Dial2verify API Key Here>";
$senderId = "<6 ALPHABETIC CHARACTERS ONLY>";
$message = "<Your SMS Text Here>";
$url = "http://host/SMS/SEND/$apiKey/$senderId/$msisdn";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('Msg' => $message));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo 'Response from server:';
print_r($result);
By using `, you are using CURL via command line.
1st, check if the CURL extension for php is installed properly on you environment. Check if something about CURL is available when you print <?php phpinfo();?>
2nd, if CURL is installed, check on php.net/curl to know how to use CURL.
Here a simple example to call your webservice :
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.host.com/SMS/SEND/$APIKey/$SenderID/$MobileNumber");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"Msg=This+is+my+text+message");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$reply = curl_exec ($ch);
curl_close ($ch);
echo $reply;
?>

cURL - require cookies and session?

I am trying to log in a website using php via cURL. But the website I am trying to log in requires cookies and session I am new to php what is the best way to set up cookies and session so that I can log in I have tried $_SESSION and setcookies they don't seem to work.. here is the code I have written any help on this topic would be helpful thanks
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/logint");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'username' => 'username',
'passwd' => 'pass',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
echo($output);
curl_close($ch);
?>
To login to a webpage using curl and get the cookie you do something like this:
curl -c cookies.txt -d "username=myUser&password=FooBar" http://xyz.com/login
and to use those cookie in later requests you do:
curl -b cookies.txt http://whatever.com/getPage
P.S: Not sure abt PHP syntax

need help converting php curl code to C language

my service provider has given me following piece of PHP code for accessing his service. I need help in converting to C lang code for use in my application. The code is using curl module to post on to a site.
pls advise.
<?php
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, "http://api.mVaayoo.com/mvaayooapi/MessageCompose?user="myusername":"mypassword"&senderID=TEST SMS&receipientno="phonenum"&msgtxt=This is a test from mVaayoo API&state=4");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=$user&senderID=$senderID&receipientno=$receipientno&cid=$cid&msgtxt=$msgtxt");
$buffer = curl_exec($ch);
if(empty ($buffer))
{ echo " buffer is empty "; }
else
{ echo $buffer; }
curl_close($ch);
?>
Use libcurl with it's C-interface. The remainer is good old C-style-string-handling.
Your example libcurl program in the comment looks good, except that for a POST you need to install a CURLOPT_READFUNCTION, not a CURLOPT_WRITEFUNCTION. But if you just want to post a static buffer, use CURLOPT_POSTFIELDS instead of a callback function.

Use curl in php script

I'm trying to create a script that will delete all user properties for a particular individual. I'm able to use an api call to get the users' properties. And I'm trying use a delete api to remove each property. But I'm having an issue doing so. Below is the code:
$delete = "http://www.ourwiki.com/#api/DELETE:users/$user_id/properties/%s";
$xml = new SimpleXMLElement($xmlString);
foreach($xml->property as $property) {
$name = $property['name']; // the name is stored in the attribute
file_get_contents(sprintf($delete, $name));
}
I believe I need to use curl to perform the actual delete. Here is an example of that command (property=something):
curl -u username:password -X DELETE -i http://ourwiki.com/#api/users/=john_smith#ourwiki.com/properties/something
-u Provides external user authentication.
-X Specifies the HTTP request method.
-i Outputs the HTTP response headers. Useful for debugging.
Is this something that I can incorporate right into the existing script? Or is there something else I need to do? Any help would be greatly appreciated.
update:
<?php
$user_id="john_smith#ourwiki.com";
$url=('http://aaron:12345#192.168.245.133/#api/deki/users/=john_smith#ourwiki.com/properties');
$xmlString=file_get_contents($url);
$delete = "http://aaron:12345#192.168.245.133/#api/deki/DELETE:users/$user_id/properties/%s";
$xml = new SimpleXMLElement($xmlString);
function curl_fetch($url,$username,$password,$method='DELETE')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // returns output as a string instead of echoing it
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); // if your server requires basic auth do this
return curl_exec($ch);
}
foreach($xml->property as $property) {
$name = $property['name']; // the name is stored in the attribute
curl_fetch(sprintf($delete, $name),'aaron','12345');
}
?>
You can use php curl, or shell out to curl using exec.
If curl is already enabled on you web server, go with php curl. If you cant install php-curl copy a command line version of curl and you are good to go.
In php-curl to set the delete method do:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
edit
Something like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.ourwiki.com/#api/whatever/url/you/want/or/need");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // returns output as a string instead of echoing it
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); // if your server requires basic auth do this
$output = curl_exec($ch);
edit2
stick that above code in a function:
function curl_fetch($url,$username,$password,$method='DELETE')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // returns output as a string instead of echoing it
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); // if your server requires basic auth do this
return curl_exec($ch);
}
and replace the call to file_get_contents() in your script with the new function.
curl_fetch(sprintf($delete, $name),'aaron','12345');
Done.
looks like you are looking for the curl function calls in php, including curl_setopt
See: http://php.net/curl

Categories