tags:

views:

100

answers:

1

Hello all,

Is there something simpler than the following.

I am trying to make a GET request to a PHP script and then exit the current script.

I think this is a job for CURL but is there something simpler as I don't want to really worry about enabling the CURL php extension?

In addition, will the below start the PHP script and then just come back and not wait for it to finish?

//set GET variables
$url = 'http://domain.com/get-post.php';

$fields = array(
    'lname'=>urlencode($last_name),
    'fname'=>urlencode($first_name)
    );

//url-ify the data for the GET
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_GET,count($fields));
curl_setopt($ch,CURLOPT_GETFIELDS,$fields_string);

//execute GET
$result = curl_exec($ch);

//close connection
curl_close($ch);

I want to run the other script which contains functions when a condition is met so a simple include won't work as the if condition wraps around the functions, right?

Please note, I am on windows machine and the code I am writing will only be used on a Windows OS.

Thanks all for any help and advice

+2  A: 
$url = 'http://domain.com/get-post.php?lname=' . urlencode($last_name) . '&fname=' . urlencode($first_name);
$html = file_get_contents($url);

If you want to use the query string assembly method (from the code you posted):

//set GET variables
$url = 'http://domain.com/get-post.php';

$fields = array(
    'lname'=>urlencode($last_name),
    'fname'=>urlencode($first_name)
    );

//url-ify the data for the GET
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
$html = file_get_contents($url . '?' . $fields_string);

See: http://php.net/manual/en/function.file-get-contents.php

karim79
Does this require a wait for the script to end?
Abs
`file_get_contents()` will not return until the request finishes processing. The OP appears to want to continue in the current PHP script before the GET request is finished, and simply ignore the results.
Amber
@Abs - do you mean the script which you are calling via GET? I'm a bit confused.
karim79
The php script that makes the request should just make the request and then die. I am using it in an AJAX request and I would like it to come back once it has started a process for the user.
Abs
@Abs - I see, you mean you do not want to capture a response, just send the request and forget about it? If that is the case, then I don't think it's possible, as HTTP is request -> response, the script will at least have to wait for a 200 response (even if there's no output).
karim79
I can make an AJAX requests with JQuery's GET request and not provide a callback function meaning I have just made a request and do not care about the response, surely this is possible?
Abs
@Abs - yes, that is possible, you just provide `null` as value `success` callback parameter.
karim79
I have used your method and re-wrote the AJAX side of my app. Thanks Karim! :)
Abs