1

I am trying to rerwite an application into php, which previously uses the $.get call in javascript, is there a way to call this in php so that i can use the response array 'answers'?

$.get('crawl.php', { text: passURL }, function(answer) {

    images = answer.images;

}, "json"); 

the only thing i found was the http_get method which doesnt seem to be responding with anything

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

Am i just calling that method wrong or is there an alternative i should be using in php?

Gazow
  • 1,019
  • 2
  • 11
  • 16
  • [Relevent](http://stackoverflow.com/questions/14745587/how-to-use-wget-in-php/14745611#14745611) – Matt Clark Nov 22 '13 at 02:50
  • @MattClark That question is a wonderful example of XY problem ... in fact, the accepted answer there makes a lot more sense in the context of PHP imho :) – Ja͢ck Nov 22 '13 at 02:55

3 Answers3

2

Try looking into curl or file_get_contents().

You'll then want to use DOMDocument (or similar) to parse the resulting DOM or, if the result is JSON use json_decode on the resulting string.

Ben D
  • 14,321
  • 3
  • 45
  • 59
0

PHP doesn't work asynchronously like JavaScript does, so you would get this (assuming a JSON response):

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

Alternatively, use cURL.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

Yes, PHP offers many ways to download data from a URL.

In fact if it has allow_url_fopen enabled in the config file, you can use pretty much any function that open files to load data from URLs aswell.

But to have a better control of headers, use POST etc. use cURL.

You may want to use json_decode() to parse the returned data the same way you do in JavaScript with JSON.parse().

Havenard
  • 27,022
  • 5
  • 36
  • 62
  • 1
    Btw, you can use POST with `file_get_contents()` too. Have a look at [`stream_context_create()`](http://php.net/stream_context_create). – Ja͢ck Nov 22 '13 at 02:53