【问题标题】:Ajax between two PHP scripts两个 PHP 脚本之间的 Ajax
【发布时间】:2015-02-19 13:59:40
【问题描述】:

我正在尝试在 2 个 PHP 脚本之间执行 AJAX 请求,其中一个只是以 json 字符串的形式返回(回显)一些数据:

[{'foo':'bar'}]

另一个只是向它发出一个简单的http请求:

$c = new http\Client;
$r = new http\Client\Request('GET', 'http://example.com/script.php');
$c->enqueue($r,
    function(http\Client\Response $r) {
            // Do something with the Json here.
            return true;
    })->send();

但我只是不知道如何从Response 对象中获取该 Json 字符串。 PECL 的documentation 根本没有帮助:

<?php

$request = new http\Client\Request("GET",
    "http://example.com",
    ["User-Agent"=>"My Client/0.1"]
);
$request->setOptions(["timeout"=>1]);

$client = new http\Client;
$client->enqueue($request)->send();

// pop the last retrieved response
$response = $client->getResponse();
printf("%s returned '%s' (%d)\n",
    $response->getTransferInfo("effective_url"),
    $response->getInfo(),
    $response->getResponseCode()
);
?>

这只是产生:

http://example.com/ 返回 'HTTP/1.1 200 OK' (200)

但仅此而已。我该怎么做?

更新:

这是使用var_dump 转储的Response 对象。

object(http\Client\Response)[6]
  protected 'type' => int 2
  protected 'body' => 
    object(http\Message\Body)[5]
  protected 'requestMethod' => string '' (length=0)
  protected 'requestUrl' => string '' (length=0)
  protected 'responseStatus' => string 'OK' (length=2)
  protected 'responseCode' => int 200
  protected 'httpVersion' => string '1.1' (length=3)
  protected 'headers' => 
    array (size=7)
      'Server' => string 'nginx/1.4.7' (length=11)
      'Date' => string 'Sat, 20 Dec 2014 23:20:07 GMT' (length=29)
      'Content-Type' => string 'text/html' (length=9)
      'Connection' => string 'keep-alive' (length=10)
      'X-Powered-By' => string 'PHP/5.5.19' (length=10)
      'X-Original-Transfer-Encoding' => string 'chunked' (length=7)
      'Content-Length' => int 1695
  protected 'parentMessage' => 
    object(http\Client\Request)[3]
      protected 'type' => int 1
      protected 'body' => 
        object(http\Message\Body)[8]
      protected 'requestMethod' => string 'GET' (length=3)
      protected 'requestUrl' => string 'http://localhost/battleturnrest/work.GetTop50.php' (length=49)
      protected 'responseStatus' => string '' (length=0)
      protected 'responseCode' => int 0
      protected 'httpVersion' => string '1.1' (length=3)
      protected 'headers' => 
        array (size=0)
          empty
      protected 'parentMessage' => null
      protected 'options' => null
  protected 'transferInfo' => 
    object(stdClass)[7]
      public 'effective_url' => string 'http://localhost/battleturnrest/work.GetTop50.php' (length=49)
      public 'response_code' => int 200
      public 'total_time' => float 0.01084
      public 'namelookup_time' => float 0.000913
      public 'connect_time' => float 0.001345
      public 'pretransfer_time' => float 0.002432
      public 'size_upload' => float 0
      public 'size_download' => float 1695
      public 'speed_download' => float 156365
      public 'speed_upload' => float 0
      public 'header_size' => int 180
      public 'request_size' => int 135
      public 'ssl_verifyresult' => int 0
      public 'filetime' => int -1
      public 'content_length_download' => float -1
      public 'content_length_upload' => float 0
      public 'starttransfer_time' => float 0.010685
      public 'content_type' => string 'text/html' (length=9)
      public 'redirect_time' => float 0
      public 'redirect_count' => int 0
      public 'connect_code' => int 0
      public 'httpauth_avail' => int 0
      public 'proxyauth_avail' => int 0
      public 'os_errno' => int 111
      public 'num_connects' => int 1
      public 'ssl_engines' => 
        array (size=0)
          empty
      public 'cookies' => 
        array (size=0)
          empty
      public 'redirect_url' => string '' (length=0)
      public 'primary_ip' => string '127.0.0.1' (length=9)
      public 'appconnect_time' => float 0
      public 'condition_unmet' => int 0
      public 'primary_port' => int 80
      public 'local_ip' => string '127.0.0.1' (length=9)
      public 'local_port' => int 45915
      public 'curlcode' => int 0
      public 'error' => string '' (length=0)

【问题讨论】:

  • 是否有$response-&gt;getBody() 函数或类似的东西?
  • 是的,但它不包含与我期望的实际响应相关的任何内容,我可能会更新问题以附加响应对象的转储。
  • 就我个人而言,当我可以使用像 jQuery 这样的简化库时,我不会对 ajax 感到头疼...... $.load 会很容易地解决你的问题。
  • 我不会说这是一个ajax 调用,它是一个服务器到服务器的请求。不涉及客户端和 javascript
  • 这一切对于你想要达到的目标来说似乎都是多余的。 $response=file_get_contents('http://example.com/script.php'); 不会做你需要的吗?

标签: php ajax json http


【解决方案1】:

这与呼叫请求场景完全相同。 main.php 调用 answer.php,它以 json 响应。

answer.php

<?php
    // the json header
    header('Content-Type: application/json');
    // parameter "value" from URL http://.../answer.php?value=bar
    $value = $_REQUEST["value"];
    // returns exactly [{'foo':'bar'}]
    echo(json_encode(array(array("foo" => $value))));
?> 

main.php

<?php
    ...
    $bar = "bar";
    $url = "http://.../answer.php?value=" . $bar;
    $arr = json_decode(file_get_contents($url));
    ...
>

最重要的是main.php是逐行执行的,你不能同时向answer.php发送两个或多个调用。

但是如果:

main.php

<?php 
// the crucial difference: asynchronous calls to the answer.php 
class Ask_For_Value extends Thread {

    public function __construct($value, $func){
        $this->val = $value;
        $this->func = $func;    
    }

    function start(){
        $arr = json_decode(file_get_contents("http://.../answer.php?value=" . $this->val));
        call_user_func($this->func, $arr);
        return(0);// boolean "OK"
    }
}

// function to process the result
function do_some_thihg_with_the_result(&$array){...}

// prepare the threads
$call1 = new Ask_For_Value("bar_1", "do_some_thihg_with_the_result");
$call2 = new Ask_For_Value("bar_2", "do_some_thihg_with_the_result");
$call3 = new Ask_For_Value("bar_3", "do_some_thihg_with_the_result");

// start the threads
$call1->start();
$call2->start();
$call3->start();

// there is nothing happens, because the threads
// will continue execution asynchronous and 
// independent in the "function do_some_thihg_with_the_result()" 

?>

【讨论】:

    猜你喜欢
    • 2019-03-26
    • 1970-01-01
    • 2013-04-19
    • 2011-08-12
    • 1970-01-01
    • 2016-11-26
    • 2015-04-03
    • 1970-01-01
    相关资源
    最近更新 更多