OMEXLU
OMEXLU

Reputation: 65

Curl-request in php > URL with parameters?

I want to know how I can make a curl-request in php with a URL including parameters ex.:

URL.tld/test.php?paramenter=test

The curl-request just must request an http visit of that URL and then close again.

Thank you in advance.

Upvotes: 2

Views: 249

Answers (1)

hNczy
hNczy

Reputation: 315

Get request for example:

<?php

$query = http_build_query([
 'param1' => 'value1',
 'param2' => 'value2',
]);

$url = "http://example.com/?".$query;

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Upvotes: 2

Related Questions