0

I was wondering can you do this call in PHP (with curl of someting?)

jQuery.ajax({
    url: this.config.url,
    type: 'GET',
    dataType: "jsonp",
    jsonp: 'callback',
    //          jsonpCallback : 'jsonp_return',
    data: {
        f:'some_function'
    }
});

Thanks in advance!

Moppo
  • 18,797
  • 5
  • 65
  • 64
  • 1
    Possible duplicate of [Send AJAX-like post request using PHP only](http://stackoverflow.com/questions/14874345/send-ajax-like-post-request-using-php-only) – thanksd Dec 22 '15 at 15:10

2 Answers2

2

If you want to make an HTTP call from PHP to a URL you can do it without using curl:

$result = file_get_contents('http://your-url');

If you need to pass some parameters to the URL you can use streams:

$data = http_build_query( ['data' => 1] );

$options =
[
    'http' =>
    [
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $data
    ]
];

$context  = stream_context_create($opts);

$result = file_get_contents('http://your-url', false, $context);

In regard to JSONP: it's used to overcome the JSON 'same origin policy' for ajax requests. As from PHP you have not this limitation and you can call any URL, you don't need the JSONP 'trick': you can simply collect the data and process it

Moppo
  • 18,797
  • 5
  • 65
  • 64
0
$data = json_decode(file_get_contents('http://you.json.url/json.php'));
var_dump($data);

See http://php.net/manual/en/function.json-decode.php for details

Eduardo
  • 7,631
  • 2
  • 30
  • 31