php 将参数发送到 URL 并从该页面获取输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4314392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send parameters to a URL and get output from that page
提问by riad
I have 2 pages say abc.phpand def.php. When abc.phpsends 2 values [id and name] to def.php, it shows a message "Value received". Now how can I send those 2 values to def.phpwithout using form in abc.phpand get the "Value received" message from def.php? I can't use form because when user frequently visits the abc.phpfile, the script should automatically work and get the message "Value received" from def.php. Please see my example code:
我有 2 页说abc.php和def.php。当abc.php向 发送 2 个值 [id 和 name] 时def.php,它会显示一条消息“收到的值”。现在如何在def.php不使用表单的情况下将这两个值发送到abc.php并从中获取“收到的值”消息def.php?我不能使用表单,因为当用户频繁访问abc.php文件时,脚本应该自动工作并从def.php. 请看我的示例代码:
abc.php:
abc.php:
<?php
$id="123";
$name="blahblah";
//need to send the value to def.php & get value from that page
// echo $value=Print the "Value received" msg from def.php;
?>
def.php:
def.php:
<?php
$id=$_GET['id'];
$name=$_GET['name'];
if(!is_null($id)&&!is_null($name))
{ echo "Value received";}
else{echo "Not ok";}
?>
Is there any kind heart who can help me solve the issue?
有没有好心人可以帮我解决这个问题?
回答by wimvds
First make up your mind : do you want GET or POST parameters.
首先下定决心:你想要GET还是POST参数。
Your script currently expects them to be GET parameters, so you can simply call it (provided that URL wrappers are enabled anyway) using :
您的脚本当前期望它们是 GET 参数,因此您可以简单地使用以下命令调用它(前提是启用了 URL 包装器):
$f = file_get_contents('http://your.domain/def.php?id=123&name=blahblah');
To use the curl examples posted here in other answers you'll have to alter your script to use $_POST instead of $_GET.
要使用其他答案中发布的 curl 示例,您必须更改脚本以使用 $_POST 而不是 $_GET。
回答by trix
You can try without cURL (I havent tried though):
您可以尝试不使用 cURL(不过我还没有尝试过):
Copy pasted from : POSTing data without cURL extension
复制粘贴自:POSTing data without cURL extension
// Your POST data
$data = http_build_query(array(
'param1' => 'data1',
'param2' => 'data2'
));
// Create HTTP stream context
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $data
)
));
// Make POST request
$response = file_get_contents('http://example.com', false, $context);
回答by Yeroon
Taken from the examples pageof php.net:
摘自php.net的示例页面:
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com/abc.php");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
Edit: To send parameters
编辑:发送参数
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( tch, CURLOPT_POSTFIELDS, array('var1=foo', 'var2=bar'));
回答by Elzo Valugi
use CURLor Zend_Http_Client.
回答by yuan3y
<?php
$method = 'GET'; //change to 'POST' for post method
$url = 'http://localhost/browse/';
$data = array(
'manufacturer' => 'kraft',
'packaging_type' => 'bag'
);
if ($method == 'POST'){
//Make POST request
$data = http_build_query($data);
$context = stream_context_create(array(
'http' => array(
'method' => "$method",
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $data)
)
);
$response = file_get_contents($url, false, $context);
}
else {
// Make GET request
$data = http_build_query($data, '', '&');
$response = file_get_contents($url."?".$data, false);
}
echo $response;
?>
get inspired by trix's answer, I decided to extend that code to cater for both GET and POST method.
受到trix 回答的启发,我决定扩展该代码以同时满足 GET 和 POST 方法。

