Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php Curl adding Params

Tags:

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.

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??

like image 564
Christopher Avatar asked Jun 16 '11 06:06

Christopher


People also ask

How do you pass multiple parameters in curl command?

How do you pass multiple query parameters in curl command? Windows user running curl binaries should use double-quotes instead of single quotes to get multiple query parameters command working.

What is Curlopt_returntransfer in curl?

CURLOPT_RETURNTRANSFER. true to return the transfer as a string of the return value of curl_exec() instead of outputting it directly. CURLOPT_SASL_IR. true to enable sending the initial response in the first packet. Added in cURL 7.31.

What is curl setopt?

The curl_setopt() function will set options for a CURL session identified by the ch parameter. The option parameter is the option you want to set, and the value is the value of the option given by the option. The value should be a long for the following options (specified in the option parameter):

What is curl_init?

The curl_init() function will initialize a new session and return a cURL handle. curl_exec($ch) function should be called after initialize a cURL session and all the options for the session are set. Its purpose is simply to execute the predefined CURL session (given by ch).


2 Answers

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.

Here is a fragment of code that does GET with some params:

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

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.

like image 123
Coder Avatar answered Oct 04 '22 08:10

Coder


You need curl_setopt() along with the CURLOPT_POSTFIELDS param. That'll POST the given params to the target page.

curl_setopt($ch, CURLOPT_POSTFIELDS, 'foo=1&bar=2&baz=3');

PS: also check http_build_query() which is handy when sending many variables.

like image 44
ChrisR Avatar answered Oct 04 '22 08:10

ChrisR