Use curl in php script - php

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

Related

Php using Curl to POST to a custom API

I'm working on a project and am attempting to link a Java application to my website/database via an API.
I'm running on an aws ec2 instance. The problem I am encountering is using Curl to POST data to another webpage. (The end-product will require proxies, and I've decided to use curl to manage this.)
The PHP code that makes the request:
<?php
function get_url($url)
{
$ch = curl_init();
if($ch === false)
{
die('Failed to create curl object');
}
// Temporary, just trying to get POST value to work.
$data = "password=temp";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data2 = curl_exec($ch);
curl_close($ch);
return $data2;
}
echo get_url("www.mywebsite.org/API/myscript.php");
?>
The server-side code that handles the request:
<?php
echo "Password: " . $_POST["password"];
?>
The issue I am having is with passing the post variables through curl and retrieving them.
The output on the page is:
Password:
and the POST variables are not being set. I'm thinking I am missing a CURLOPT somewhere but can't figure out which one!

How to get variable value from cURL

<?php
$message=$_POST["msg"];
echo $message;
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://.../pl1.php?
m=$message");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
Above is the code iam using.anyone suggest me how do i get value of m in pl1.php
I am getting following error:
Your browser sent a request that this server could not understand.
In PHP variables passed as query string inside URL are accessible in $_GET array.
In your case it will be $_GET['m'].
Try this code:
<?php
$message=$_POST["msg"];
echo $message;
// curl initialize
$ch = curl_init();
// set curl options
curl_setopt($ch, CURLOPT_URL, "http://.../pl1.php?
m=$message");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// execute request and return response
$output = curl_exec($ch);
echo $output;
// close cURL request
curl_close($ch);
?>
And to get that data on page pl1.php use $_GET['m'];
But, I believe sending data using curl with post method is better than get method.
Thank you

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;
?>

How to use GET on an external URL

I am using an API that returns JSON from a GET request
Eg.
https://api.domain.com/v1/Account/{auth_id}/Call/{call_uuid}
Returns
{
"call_duration": 4,
"total_amount": "0.00400"
}
How can I call this page from within a script and save call_duation and total_amount as separate variables?
Something like the following?:
$call_duration =
$_GET[https://api.domain.com/v1/Account/{auth_id}/Call/{call_uuid}, 'call_duration'];
$_GET[] contains the get parameters that are passed to your code - they don't generate a GET request.
You could use curl to make your request:
$ch = curl_init("https://api.domain.com/v1/Account/{auth_id}/Call/{call_uuid}");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$result = json_decode($output);
If PHP has allow_url_fopen enabled you can simply do
json_decode(file_get_contents('https://api.domain.com/v1/Account/{auth_id}/Call/{call_uuid}'))
Otherwise you'll have to resort to using something like Curl to get the request going. $_GET is a superglobal array which doesn't actually do anything. It only contains what the script was started with. It does not make any requests itself.
Use curl to get the JSON, then json_decode to decode it into PHP variables
$auth_id = 'your auth id here';
$call_uuid = 'your call_uuid here';
// initialise curl, set URL and options
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.domain.com/v1/Account/{$auth_id}/Call/{$call_uuid}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// get the response and decode it
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
$call_duration = $response['call_duration'];
$total_amount = $response['total_amount'];

sending xml string using php curl but not as post parameter

:)
I'm trying to send an XML using curl but not as post parameter. what I mean is this.
for example.
the receiving side of that XML won't be able to recieve the XML using $_POST variable.
he will need to use the following code:
$xmlStr=null;
$file=fopen('php://input','r');
$xmlStr=fgets($file);
I want to be able to send an xml string using curl via https.
so the following would be wrong:
public static function HttpsNoVerify($url,$postFields=null,$verbose=false) {
// Initialize session and set URL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ($postFields !=null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
if ($verbose) {
curl_setopt($ch, CURLOPT_VERBOSE, 1);
}
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
because here i can use HttpsNoVerify($url,array('xml_file'=>'xml..')); and that
will paste it as post parameter. and i want it as post output.
so please I hope i explained myself properly and I explained exactly what I don't want to do.
how can I do what i want to do?
thanks! :)
kfir
Just directly pass the xml string as second parameter instead of an associative array item,
HttpsNoVerify($url, 'xml ..');
This will eventually call
curl_setopt($ch, CURLOPT_POSTFIELDS, "xml ...");
Which will be put in php://input for the remote server.

Categories