0

I have been advised to use this in my script

        curl -X POST https://api.twilio.com/2010-04-01/Accounts/HIDDEN/SMS/Messages.json \
        -u HIDDEN\
        -d "From=+442033228389" \
        -d "To=hidden" \
        -d 'Body=test'

But a simple cut and paste does not do the trick? How would I go about incorporating this into my script?

Results:

var_dump($output); returns: bool(false)

var_dump($info); returns:

array(26) { ["url"]=> string(95) "https://api.twilio.com/2010-04-01/Accounts/AC7ae43150d51cce16de4be6ed0be5ca90/SMS/Messages.json" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.093) ["namelookup_time"]=> float(0) ["connect_time"]=> float(0.093) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["redirect_url"]=> string(0) "" ["primary_ip"]=> string(15) "174.129.254.101" ["certinfo"]=> array(0) { } ["primary_port"]=> int(443) ["local_ip"]=> string(11) "192.168.0.2" ["local_port"]=> int(28469) }

user1968541
  • 333
  • 1
  • 3
  • 12

2 Answers2

1

If you want to exec shell command from inside a PHP script you'll have to use one of the functions exec, shell_exec, system or proc_open or simply the backtick operator `

$output = `curl -X POST https://api.twilio.com/2010-04-01/Accounts/HIDDEN/SMS/Messages.json -u HIDDEN -d "From=+442033228389" -d "To=hidden" -d 'Body=test'`;

But if you want to use curls functionality with PHP the better way would be to use the curl extension. Here comes an example:

<?php

// check if the curl extension is available
if(!function_exists('curl_init')) {
    die('the curl extension is not installed');
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.twilio.com/2010-04-01/Accounts/HIDDEN/SMS/Messages.json');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "From=+442033228389\nTo=hidden\nBody=test");

$result = curl_exec($ch);

// json_decode is used to translate the result into an object
var_dump(json_decode($result));
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
1
<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.twilio.com/2010-04-01/Accounts/HIDDEN/SMS/Messages.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);

$data = array(
    'From' => '+442033228389',
    'To' => 'hidden',
    'Body' => 'test'
);
/* // WHERE $username = your account username
   // Where $password = Your account password
*/
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
Will H
  • 1,408
  • 1
  • 13
  • 20
  • I'm sending data to a web service not pulling data – user1968541 Feb 25 '13 at 02:28
  • 1
    Sending data follows the same path, you just change it to a *Post* for instance. Look here for more details. You will still be using the same library... I'm updating my answer with details. – Will H Feb 25 '13 at 02:33
  • 1
    That should do the trick based on your example, based on the input that the api requires you may just need to json_encode that array before you add it to the curl_setopt – Will H Feb 25 '13 at 02:35
  • Just ran the example you given me, updating the needed information but i still did not recieve my expected notification – user1968541 Feb 25 '13 at 02:38
  • When you run that example do you: var_dump($output); var_dump($info); – Will H Feb 25 '13 at 02:39
  • 1
    Looks like you are missing your authentication. Edit your original question and remove your account URL items, so everyone doesn't see it. Also you will need to add your auth items which I'll edit in the example above see comments. – Will H Feb 25 '13 at 02:45
  • $info just gives transaction information did the $output show you any data? If your authentication was incorrect then it should still give you Authenticate errors. If thats the case then you'll have to find out from Twillio what the error is or if they can help you, but beyond that my example and the example from @hek2mgl is perfectly valid, minus the fact that he doesn't have authentication both should work just fine. – Will H Feb 25 '13 at 02:58
  • Try changing your post data with what the previous @hek2mgl noted with: curl_setopt($ch, CURLOPT_POSTFIELDS, "From=+442033228389\nTo=hidden\nBody=test"); Beyond that I cannot really help much more as everything else is valid curl wise. – Will H Feb 25 '13 at 03:08
  • I tried as an attempt moving it over to my hosting account and your example works.. I was testing from xampp with perl and curl installed and sucessfully configured.. It is a problem on my servers end, not from the code. – user1968541 Feb 25 '13 at 03:34