Php Curl 添加参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6367761/
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
Php Curl adding Params
提问by Christopher
Im a newbie im trying to get a script to trigger another script with Curl in PHP but it dosent seem to be sending the paramaters.
我是一个新手,我正在尝试使用 PHP 中的 Curl 获取一个脚本来触发另一个脚本,但它似乎正在发送参数。
Is there a seperate function to append parameters?
是否有单独的函数来附加参数?
<?php
$time = time();
$message = "hello world";
$urlmessage = urlencode( $message );
$ch = curl_init("http://mysite.php?message=$urlmessage&time=$time");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
Could anyone point me in the right direction??
有人能指出我正确的方向吗?
回答by ChrisR
You need curl_setopt()along with the CURLOPT_POSTFIELDS param. That'll POST the given params to the target page.
您需要curl_setopt()以及 CURLOPT_POSTFIELDS 参数。这会将给定的参数发布到目标页面。
curl_setopt($ch, CURLOPT_POSTFIELDS, 'foo=1&bar=2&baz=3');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'foo=1&bar=2&baz=3');
PS: also check http_build_query()which is handy when sending many variables.
PS:还要检查http_build_query()这在发送许多变量时很方便。
回答by Coder
The accepted answer is good for POST, but what if OP wanted specifically to GET? Some REST APIs specify the http method and often it's no good POSTing when you should be GETting.
接受的答案对 POST 有好处,但是如果 OP 专门想要 GET 怎么办?一些 REST API 指定了 http 方法,并且通常在您应该 GET 时进行 POST 是不好的。
Here is a fragment of code that does GET with some params:
这是一段使用一些参数执行 GET 的代码片段:
$endpoint = 'http://example.com/endpoint';
$params = array('foo' => 'bar');
$url = $endpoint . '?' . http_build_query($params);
curl_setopt($ch, CURLOPT_URL, $url);
This will cause your request to be made with GET
to http://example.com/endpoint?foo=bar
. This is the default http method, unless you set it to something else like POST
with curl_setopt($ch, CURLOPT_POST, true)
- so don't do that if you specifically need to GET.
这将导致您的请求使用GET
to http://example.com/endpoint?foo=bar
。这是默认的 http 方法,除非您将其设置为类似POST
with 的其他方法curl_setopt($ch, CURLOPT_POST, true)
- 所以如果您特别需要 GET,请不要这样做。
If you need to use one of the other http methods (DELETE or PUT for example) then use curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method)
. This also works for GET and POST.
如果您需要使用其他 http 方法之一(例如 DELETE 或 PUT),请使用curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method)
. 这也适用于 GET 和 POST。
回答by The Mask
you need set CURLOPT_POST
as true
and CURLOPT_POSTFIELDS
=> parameters
您需要设置CURLOPT_POST
为true
和CURLOPT_POSTFIELDS
=> 参数
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
a suggestion,set 'CURLOPT_RETURNTRANSFER
', as true to return the transfer as a string of the return value of curl_exec($ch)
instead of outputting it out directly
一个建议,设置 ' CURLOPT_RETURNTRANSFER
', as true 将传输作为返回值的字符串返回,curl_exec($ch)
而不是直接输出