views:

52

answers:

1

I am trying to run a PHP bot function in a Safari extension.

This is the HTML code I've tried to put together, but it's a mess of PHP, cURL, and HTML, and I don't really know what I'm doing.

I've never attempted to use cURL before I got this code from http://www.barattalo.it/2010/08/09/send-push-notification-to-iphone-with-php-and-pushme-to/, and it seems like the extension stops working when I try to initiate $ch. That obviously defeats the whole purpose of the code, because I need it to run the cURL commands.

Any tips to get this working?

 function performCommand(event)
 {    if (event.command === "sendLink") { 
pushMeTo($u,$t,$s);      }


 function pushMeTo($widgeturl, $text, $signature)
 { $agent = "Mozilla/5.0 (windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12";
  $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $widgeturl);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  $page = curl_exec($ch);
   preg_match("/form action=\"(.*?)\"/", $page, $form_action);
   preg_match("/textarea name=\"(.*?)\"/", $page, $message_field);
   preg_match("/input type=\"text\" name=\"(.*?)\"/", $page, $signature_field);

  $ch = curl_init();

  $strpost = $message_field[1].'=' . urlencode($text) . '&'.$signature_field[1].'=' . urlencode($signature);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $strpost );
   curl_setopt($ch, CURLOPT_URL, $form_action[1]);
   curl_setopt($ch, CURLOPT_POST, 1);
   curl_setopt($ch, CURLOPT_HEADER, 0);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_USERAGENT, $agent);    
  $page = curl_exec($ch);
 }
+1  A: 

You won't be able to do what you want the way that you're doing it. In his comment, zneak is absolutely right. You're extension is running on the client-side while PHP runs server side. Your Javascript has no idea what to make of your PHP code.

As an alternative, your extension can fire off an Ajax request to a URL that can run the PHP code. I do this in one of my extensions where I have to make a service call. Works great.

Rob Wilkerson