ajax() public static method

Make an AJAX request.
public static ajax ( $options = [], $xhr = null ) : Zend_Http_Client
return Zend_Http_Client
Example #1
0
 function __construct($data)
 {
     $pq = null;
     include_once dirname(__FILE__) . '/../phpQuery/phpQuery.php';
     if (file_exists(dirname(__FILE__) . '/jQueryServer.config.php')) {
         include_once dirname(__FILE__) . '/jQueryServer.config.php';
         if ($jQueryServerConfig) {
             $this->config = array_merge_recursive($this->config, $jQueryServerConfig);
         }
     }
     if ($this->config['refererMustMatch']) {
         foreach ($this->config['allowedRefererHosts'] as $i => $host) {
             if ($host == '.') {
                 $this->config['allowedRefererHosts'][$i] = $_SERVER['HTTP_HOST'];
             }
         }
         $referer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
         $authorized = $referer && in_array($referer, $this->config['allowedRefererHosts']);
         if (!$authorized) {
             throw new Exception("Host '{$_SERVER['HTTP_REFERER']}' not authorized to make requests.");
             return;
         }
     }
     //		phpQueryClass::$debug = true;
     //		if (! function_exists('json_decode')) {
     //			include_once(dirname(__FILE__).'/JSON.php');
     //			$this->json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
     //		}
     //		$data = $this->jsonDecode($data);
     $data = phpQuery::parseJSON($data);
     // load document (required for first $data element)
     if (is_array($data[0]) && isset($data[0]['url'])) {
         $this->options = $data[0];
         $ajax = $this->options;
         $this->calls = array_slice($data, 1);
         $ajax['success'] = array($this, 'success');
         phpQuery::ajax($ajax);
     } else {
         throw new Exception("URL needed to download content");
         break;
     }
 }
Example #2
0
 /**
  * Enter description here...
  *
  * @link http://docs.jquery.com/Ajax/load
  * @return phpQuery|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
  * @todo Support $selector
  */
 public function load($url, $data = null, $callback = null)
 {
     if ($data && !is_array($data)) {
         $callback = $data;
         $data = null;
     }
     if (mb_strpos($url, ' ') !== false) {
         $matches = null;
         if (extension_loaded('mbstring') && phpQuery::$mbstringSupport) {
             mb_ereg('^([^ ]+) (.*)$', $url, $matches);
         } else {
             preg_match('@^([^ ]+) (.*)$@', $url, $matches);
         }
         $url = $matches[1];
         $selector = $matches[2];
         // FIXME this sucks, pass as callback param
         $this->_loadSelector = $selector;
     }
     $ajax = array('url' => $url, 'type' => $data ? 'POST' : 'GET', 'data' => $data, 'complete' => $callback, 'success' => array($this, '__loadSuccess'));
     phpQuery::ajax($ajax);
     return $this;
 }
 public static function getJSON($url, $data = null, $callback = null)
 {
     if (!is_array($data)) {
         $callback = $data;
         $data = null;
     }
     // TODO some array_values on this shit
     return phpQuery::ajax(array('type' => 'GET', 'url' => $url, 'data' => $data, 'success' => $callback, 'dataType' => 'json'));
 }
Example #4
0
 /**
  * Enter description here...
  *
  * @param unknown_type $e
  * @TODO trigger submit for form after form's  submit button has a click event
  */
 public static function handleSubmit($e, $callback = null)
 {
     $node = phpQuery::pq($e->target);
     if (!$node->is('form') || !$node->is('[action]')) {
         return;
     }
     // TODO document.location
     $xhr = isset($node->document->xhr) ? $node->document->xhr : null;
     $submit = pq($e->relatedTarget)->is(':submit') ? $e->relatedTarget : $node->find('*:submit:first')->get(0);
     $data = array();
     foreach ($node->serializeArray($submit) as $r) {
         // XXXt.c maybe $node->not(':submit')->add($sumit) would be better ?
         //		foreach($node->serializeArray($submit) as $r)
         $data[$r['name']] = $r['value'];
     }
     $options = array('type' => $node->attr('method') ? $node->attr('method') : 'GET', 'url' => resolve_url($e->data[0], $node->attr('action')), 'data' => $data, 'referer' => $node->document->location);
     if ($node->attr('enctype')) {
         $options['contentType'] = $node->attr('enctype');
     }
     $xhr = phpQuery::ajax($options, $xhr);
     if ((!$callback || !$callback instanceof Callback) && $e->data[1]) {
         $callback = $e->data[1];
     }
     if ($xhr->getLastResponse()->isSuccessful() && $callback) {
         phpQuery::callbackRun($callback, array(self::browserReceive($xhr)));
     }
 }
Example #5
0
//		print "</pre>\n";
//	}
//	print "\n";
//}
$testName = 'Load';
$test = phpQuery::newDocumentFile('test.html')->find('div:first')->load('http://wikipedia.org/ div[lang]');
if (pq('div[lang]')->size()) {
    print "Test '{$testName}' PASSED :)";
} else {
    print "Test '{$testName}' <strong>FAILED</strong> !!! ";
    print "<pre>";
    print "</pre>\n";
}
print "\n";
// http://code.google.com/p/phpquery/issues/detail?id=130
$pq = phpQuery::ajax(array('url' => 'http://' . $_SERVER['SERVER_NAME'] . preg_replace('@/[^/]+$@', '/test_ajax_data_1', $_SERVER['REQUEST_URI']), 'success' => 'a789fhasdui3124', 'error' => 'jhdbg786213u8dsfg7y'));
function a789fhasdui3124($html)
{
    $testName = 'AJAX request text node';
    if ($html == 'hello world') {
        print "Test '{$testName}' PASSED :)";
    } else {
        print "Test '{$testName}' <strong>FAILED</strong> !!! ";
    }
    print "\n";
}
function jhdbg786213u8dsfg7y()
{
    $testName = 'AJAX request text node';
    print "Test '{$testName}' <strong>FAILED</strong> !!! ";
}
 public static function post($url, $data = null, $callback = null, $type = null)
 {
     if (!is_array($data)) {
         $callback = $data;
         $data = null;
     }
     return phpQuery::ajax(array('type' => 'POST', 'url' => $url, 'data' => $data, 'success' => $callback, 'dataType' => $type));
 }