Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
两个PHP脚本之间的Ajax_Php_Ajax_Json_Http - Fatal编程技术网

两个PHP脚本之间的Ajax

两个PHP脚本之间的Ajax,php,ajax,json,http,Php,Ajax,Json,Http,我尝试在两个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) {

我尝试在两个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();
但我就是不知道如何从响应对象获取Json字符串。PECL一点帮助都没有:

<?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()
);
?>

这与呼叫请求场景完全相同。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))));
?> 

是否有$response->getBody函数或类似的函数?是的,但它不包含任何与我期望的实际响应相关的内容,我可能会更新问题以附加响应对象的转储。就我个人而言,当我可以使用简化的库(如jQuery…$)时,我不会对ajax动脑。load会很容易解决您的问题。我不会说这是一个ajax调用,而是一个服务器到服务器的请求。没有涉及客户端和javascript这一切对于您试图实现的目标来说似乎都是杀伤力过大。不会$response=文件\u获取\u内容'http://example.com/script.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))));
?> 
<?php
    ...
    $bar = "bar";
    $url = "http://.../answer.php?value=" . $bar;
    $arr = json_decode(file_get_contents($url));
    ...
>
<?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()" 

?>