Re: curl and variable parameters in hyperlink
| From: | Daniel Molina Wegener | Date: | Wed, 24 Nov 2010 09:06:25 +0000 |
| Subject: | Re: curl and variable parameters in hyperlink | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-309649@lists.php.net to get a copy of this message | ||
On Wednesday 24 November 2010,
"Bob Keightley" <bob.keightley@virgin.net> wrote:
> I already have a curl script that gets the web page, but it doesn't pass
> the parameters
Hello Bob,
>
> Being new to PHP I haven't the first idea how to modify it so that it
> does.
>
> Script is as follows:
>
> $url =
> "http://www.xxxxxx.com/query.asp?param1=val1¶m2=val2";
>
> foreach ($_POST as $key=>$post) {
> $post=str_replace(" ", "+", $post);
> $url.=$key."=".$post."&";
> }
Instead of concatenating strings, I suggest to use the http_build_query()
function:
<?php
$data = array('param1' => 'val1',
'param2' => 'val2',
'param3' => 'val3');
$query = http_build_query($data);
echo "Query: {$query}\n";
?>
So, to create a query string based on your $_POST request parameters,
you just need to use the function as follows:
<?php
$query = http_build_query($_POST);
echo "Query: {$query}\n";
?>
This will create the proper query string to be used on your URL.
>
> $ch = curl_init($url);
> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
> curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
> $data = curl_exec($ch);
> curl_close($ch);
>
> $data=str_replace('.asp', '.php', $data);
> echo $data;
>
> This returns the web page, but ignores val1 and val2 which are necessary
> to execute the query.
Best regards,
--
Daniel Molina Wegener <dmw [at] coder [dot] cl>
System Programmer & Web Developer
Phone: +56 (2) 979-0277 | Blog: http://coder.cl/
Attachment: [application/pgp-signature] This is a digitally signed message part. signature.asc
Attachment: [application/pgp-signature] This is a digitally signed message part. signature.asc