3

I am trying to pass variable value (sample=1000) to a link

http://10.219.5.109:9000/mean

and it should finally look like

http://10.219.5.109:9000/mean?sample=1000 

I am using following php code but it seems variable are not getting passed properly as it not running.

$curl_connection =curl_init('http://10.219.5.109:9000/mean');
$post_string='sample=1000';
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($curl_connection);
print_r(curl_getinfo($curl_connection));

How can i properly bind variables to link?

3
  • 1
    $curl_connection = curl_init('http://10.219.5.109/mean?'.$post_string) Commented Feb 13, 2017 at 14:07
  • The last .'' are redundant @Kisaragi Commented Feb 13, 2017 at 14:07
  • 1
    @Albzi Thanks, updated Commented Feb 13, 2017 at 14:08

2 Answers 2

5

It seems that you are doing a GET request, so the post variables will never be send to the server. You could just add the parameters as a query string to the url.

Like so:

$curl_connection = curl_init('http://10.219.5.109:9000/mean?sample=1000');
$result = curl_exec($curl_connection);
print_r(curl_getinfo($curl_connection));

If you want the data to be dynamic, you can do it like this:

$post_string = 'sample=1000';
$curl_connection = curl_init('http://10.219.5.109:9000/mean?' . $post_string);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks!!..It working perfectly.. But how can i bind more variables in it ? I have 4 variables "size", "height", "weight" and "class". Can you please help me with this?
Yes, you just have to put an & between them. So it becomes sample=1000&size=15&height=0 etc.
0
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://10.219.5.109:9000/mean?sample=1000',
CURLOPT_USERAGENT => 'Simple cURL'
));
$resp = curl_exec($curl);
curl_close($curl);
?>

Sample value can be also set through variable.

Comments

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.