1

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

1 Answer 1

6
$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

Sign up to request clarification or add additional context in comments.

9 Comments

Does this require a wait for the script to end?
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.
@Abs - do you mean the script which you are calling via GET? I'm a bit confused.
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 - 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).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.