从PHP脚本发出PHP GET请求并退出 [英] Make a PHP GET request from a PHP script and exit
问题描述
有什么比下面更简单的东西了。
Is there something simpler than the following.
我正在尝试向PHP脚本发出GET请求,然后退出当前脚本。
I am trying to make a GET request to a PHP script and then exit the current script.
我认为这是CURL的工作,但是有没有更简单的东西,因为我不想真正担心启用CURL php扩展?
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?
此外,下面的代码是否将启动PHP脚本,然后再返回而不等待它完成?
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);
我想在条件满足时运行另一个包含函数的脚本,这样一个简单的include won'
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?
请注意,我在Windows计算机上,编写的代码仅在Windows OS上使用。
Please note, I am on windows machine and the code I am writing will only be used on a Windows OS.
谢谢大家的帮助和建议
推荐答案
$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);
请参阅:
http://php.net/manual/zh/function.file-get-contents.php
这篇关于从PHP脚本发出PHP GET请求并退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!