当前位置: 动力学知识库 > 问答 > 编程问答 >

Ajax between two PHP scripts

问题描述:

I'm tring to perform an AJAX request between 2 PHP scripts, one of them just returns (echoes) some data in the form of a json string:

[{'foo':'bar'}]

And the other one just makes a simple http request to it:

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

But I just can't figure out how to get that Json string from the Response object. PECL's documentation doesn't help at all:

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

);

?>

This just yields:

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

But nothing more. How can I do this?

Update:

This is a dump of the Response object using var_dump.

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)

网友答案:

This is exactly the same call-request scenario. The main.php calls answer.php, and it answers with 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));
    ...
>

The most important thing is main.php executes line by line and you can not send two or more calls to the answer.php in the same moment.

But if:

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()" 

?>
分享给朋友:
您可能感兴趣的文章:
随机阅读: