简体   繁体   English

如何在Safari扩展程序中运行带有cURL代码的PHP脚本?

[英]How can I run a PHP script with cURL code in a Safari extension?

I am trying to run a PHP bot function in a Safari extension. 我正在尝试在Safari扩展中运行PHP机器人功能。

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. 这是我试图组合在一起的HTML代码,但是它是一堆PHP,cURL和HTML,我真的不知道自己在做什么。

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. 在从http://www.barattalo.it/2010/08/09/send-push-notification-to-iphone-with-php-and-pushme-to/获得此代码之前,我从未尝试使用cURL ,而当我尝试启动$ ch时,该扩展似乎停止工作。 That obviously defeats the whole purpose of the code, because I need it to run the cURL commands. 这显然破坏了代码的全部目的,因为我需要它来运行cURL命令。

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

You won't be able to do what you want the way that you're doing it. 您将无法按照自己的方式做自己想做的事。 In his comment, zneak is absolutely right. 在他的评论中,zneak是绝对正确的。 You're extension is running on the client-side while PHP runs server side. 您的扩展程序在客户端运行,而PHP在服务器端运行。 Your Javascript has no idea what to make of your PHP code. 您的Javascript不知道如何编写PHP代码。

As an alternative, your extension can fire off an Ajax request to a URL that can run the PHP code. 或者,您的扩展程序可以将Ajax请求触发到可以运行PHP代码的URL。 I do this in one of my extensions where I have to make a service call. 我在我的一个扩展中执行此操作,我必须拨打服务电话。 Works great. 效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM