PHP中是否有类似于$.get()方法的方法


Is there an equivalent of the $.get() method in PHP?

我正在尝试将一个应用程序重新编写到php中,该应用程序以前在javascript中使用$.get调用,有没有办法在php中调用它,以便我可以使用响应数组"answerswers"?

$.get('crawl.php', { text: passURL }, function(answer) {
    images = answer.images;
}, "json"); 

我唯一发现的是http_get方法,它似乎没有任何响应

$response = http_get('crawl.php', array( text=> $passURL), $answer);
$images = $answer['images'];

我只是错误地调用了那个方法,还是我应该在php中使用其他方法?

尝试查看curlfile_get_contents()

然后,您将希望使用DOMDocument(或类似的)来解析生成的DOM,或者,如果结果是JSON,则对生成的字符串使用json_decode

PHP不像JavaScript那样异步工作,所以您会得到以下信息(假设是JSON响应):

$answer = json_decode(
    file_get_contents('http://host/path/crawl.php?' . http_build_query([
        'text' => $passUrl
    ])), 
    true
);

或者,使用cURL。

是的,PHP提供了许多从URL下载数据的方法。

事实上,如果它在配置文件中启用了allow_url_fopen,那么几乎可以使用任何打开文件的函数来从URL加载数据。

但是为了更好地控制标题,请使用POST等。请使用cURL。

您可能希望使用json_decode()来解析返回的数据,就像使用JSON.parse()在JavaScript中一样。