38

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

3
  • Please read through PHP's documentation: us3.php.net/manual/en/book.curl.php. This issue is explained in a fair amount of detail. Commented Jun 16, 2011 at 6:23
  • Don't do this; $ch = curl_init("http://mysite.php?message=$urlmessage&time=$time"); Do this; $ch = curl_init('http://mysite.php?message=' . $urlmessage . '&time=' . $time); It's much easier to read, and you'll be a byte/8 safer. What if you had a variable $url, it could theoreticaly be used instead of $urlmessage. Commented Jun 16, 2011 at 6:23
  • 1
    @Philiplip: Even better, use sprintf(). Like this: sprintf('mysite.com/myscript.php?message=%s&time=%d', $urlmessage, $time); (EDIT:....can't seem to figure out how to add code pieces in comments....:S) Commented Jun 16, 2011 at 6:29

4 Answers 4

84

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.

Sign up to request clarification or add additional context in comments.

1 Comment

28

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.

5 Comments

thanks heaps the only problem with that is that it will not allow my variables to post as values or strings instead it post them as literals i.e. $time should = 5327287 but $time = "$time" instead. Thanks though
@Christopher: Change the single quotes to double quotes, that will fix the issue of the literals. Also, put the POST values string in a variable, instead of building it inside the curl_setopt function call. This is because it's generally a good idea to send POST length with the curl as well, and then you don't have to build the query twice.
Is there anyway for the script to run but then for it to bounce back to the original?
@chrisR how to use those values in the triggered php file on the server side?
@WalidSarkis using CURLOPT_POSTFIELDS just creates a POST http request with POST params so you can access them on the receiving end from the $_POST global in php
13

you need set CURLOPT_POST as true and CURLOPT_POSTFIELDS => parameters

  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

2 Comments

This is a much better solution to the accepted. Its better to set CURLOPT_POSTFIELDS to an array of parameters than to build a query string version.
Tricky fact is that CURLOPT_POSTFIELDS internally automatically sets the CURLOPT_POST to true, so the above might be redundant.
-2

Here is A Simple Solution for this.

 $mobile_number = $_POST['mobile_number'];
 $sessionid = $_POST['session_id'];
    CURLOPT_URL => 'https://xxyz.jkl.com/v2.0/search?varible_that_you_want_to_pass='.$mobile_number.'&requestId=1616581154955&locale=en-US&sessionId='.$sessionid,

1 Comment

Never just use data directly from the $_POST var like that, its not secure in the slightest. Always sanitise it first. As a quick & dirty test, ok - but really, even then, don't. Get in the habit of doing it securely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.