簡體   English   中英

從PHP腳本發出PHP GET請求並退出

[英]Make a PHP GET request from a PHP script and exit

有沒有比以下更簡單的方法。

我正在嘗試向PHP腳本發出GET請求,然后退出當前腳本。

我認為這是CURL的工作,但是有沒有更簡單的事情,因為我不想真正擔心啟用CURL php擴展?

此外,下面的代碼是否會啟動PHP腳本,然后返回而不等待它完成?

//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);

我想在滿足條件時運行包含功能的其他腳本,因此如果條件環繞功能,那么簡單的包含將不起作用,對嗎?

請注意,我在Windows計算機上,我編寫的代碼僅在Windows操作系統上使用。

謝謝大家的幫助和建議

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

如果要使用查詢字符串匯編方法(來自發布的代碼):

//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/en/function.file-get-contents.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM