Attaching get fields to URL using Curl in PHP
11,618
Try this:
// Fill in your DATA below.
$data = array('param' => "datata", 'param2' => "HelloWorld");
/*
* cURL request
*
* @param $url string The url to post to 'theurlyouneedtosendto.com/m/admin'/something'
* @param $req string Request type. Ex. 'POST', 'GET' or 'PUT'
* @param $data array Array of data to be POSTed
* @return $result HTTP resonse
*/
function curl_req($url, $req, $data='')
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $req);
if (is_array($data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Fill in your URL below.
$result = curl_req("http://yourURL.com/?", "POST", $data)
echo $result;
This works fine for me.
Author by
JoelS
Updated on June 04, 2022Comments
-
JoelS almost 2 years
I am able to perform server and client side redirects using Curl but I am unable to attach GET fields to the URL via a get request, here is my code:
$post = curl_init(); curl_setopt($post,CURLOPT_URL,$url); curl_setopt($post,CURLOPT_RETURNTRANSFER,TRUE); curl_setopt($post, CURLOPT_USERAGENT,'Codular'); curl_setopt($post, CURLOPT_CUSTOMREQUEST,'GET'); curl_exec($post); curl_close($post);
Nothing gets attached when I perform the execution, what am I doing wrong?
New code I am using:
function curl_req($url, $req, $data='') { $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $req); if (is_array($data)) { curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); return $result; } $temp = array("akash"=>"test"); $result = curl_req("http://localhost/test.php", 'POST', $temp); echo $result; print_r($result);
test.php:
print_r($_POST); print_r($_REQUEST);
-
Klemen Tusar over 8 yearsyou should rather use
http_build_query()
and pass it an array instead of doing your GET query manually -
JoelS over 8 yearsnope instead i get this: You must pass either an object or an array with the CURLOPT_HTTPHEADER
-
Bolli over 8 years@JoelS I updated the my answer. Does it work for you now?
-
JoelS over 8 yearsstill nothing gets attached to the URL
-
JoelS over 8 yearsthis did it: print("<a href=\"Profile.php?$params\">Example</a>");, im not sure this is an official way of doing it though.
-
Klemen Tusar over 8 yearsThat's a httpd configuration problem ... :P
-
Bolli over 8 yearsIt is something at the URL's server end that is wrong then. If you create another PHP file and add: print_r($_GET); and then request that file using my function, you can clearly see it is sending the GET requests. When using in php.net I get client denied error. If you post what GET params you are sending, I might be able to help further.
-
JoelS over 8 yearsthe get params were name.name.4
-
JoelS over 8 yearsi got this when you told me to check for the get parameters being sent: Array ( [param] => datata [param2] => hekloworld )
-
JoelS over 8 yearsthats a shame i really wanted to use cURL for this as well
-
Bolli over 8 years@JoelS Maybe you can change your http headers and make it work. Can you post the entire URL with GET parameters you are trying to get.
-
JoelS over 8 yearsis it possible to do the same thing but as a post/serverside-redirect?
-
Bolli over 8 years@JoelS the reply you get from my script above when calling your own php file - is correct - so it is send as GET. In the $data array you can change the parameters and see it works.
-
Bolli over 8 years@JoelS yes you can do it with POST as well, I can update the answer if you like. But I don't know if it works on the real URL you are trying to POST/GET to.
-
JoelS over 8 yearsyes ive tried it with a search script i use in php. so its possible to do a server side redirect liker header() using cURL in php?
-
Bolli over 8 yearsIm not completely sure what you mean? But you can send POST requests easily with cURL in PHP. Is this what you want?
-
JoelS over 8 yearsas in header() is a server side redirect, im just wondering whether you can do something very similar using cURL instead
-
Bolli over 8 years@JoelS Yes I think you need to search for 'php curl proxy' - I found this: stackoverflow.com/questions/5211887/how-to-use-curl-via-a-proxy and this: stackoverflow.com/questions/16934409/…
-
JoelS over 8 years@Bolli can you show me how to do the equivalent via a POST method please?
-
Bolli over 8 years@JoelS I updated my post. This function works for both POST and get, simply change the $req parameter to what you need.
-
Bolli over 8 years@JoelS Try now - I removed the json decode line. It works for me. Remember if you are testing locally print_r($_GET] wont work. You need to change it to print_r($_POST) or print_r($_REQUEST) which handles both POST and GET.
-
JoelS over 8 yearsstill nothing on the page
-
Bolli over 8 years@JoelS Did you copy all of it? It works fine here, I think you are not using it correct then? Update your post with your code, else its hard for me to help
-
JoelS over 8 yearsyes i did copy all of it and its displays on the same page not the test.php which is what im looking for.
-
Bolli over 8 years@JoelS that looks fine to me? Do you get blanc page? It should output the POST data you sent?
-
JoelS over 8 yearsyeh i got a blank page
-
Bolli over 8 years@JoelS when I copy paste your code I get the correct result. So thats strange! Do you have curl enabled on your localhost?
-
JoelS over 8 yearsyes. it worked with the get request, just the post is the problem
-
Bolli over 8 years@JoelS Strange it works fine here. I get this output Array ( [akash] => test ) and I send using POST. Hmm.. I'm almost out of ideas.. and you are 100% sure the location of your test.php file is correct?
-
JoelS over 8 yearsyes the location is correct im sure of that. thats very weird why is playing around like that. mmmm
-
JoelS over 8 yearsi dont get anything this is so frustrating
-
Bolli over 8 years@ very strange indeed. And you are 100% sure cURL is enabled? You can test it by adding this at the top of your curl script file: if (!function_exists('curl_init')) { echo "cURL is not enabled"; }
-
JoelS over 8 yearsyes nothing prints out as expected. when i do a var_dump($_SERVER) i get a request method is get, how do i change that or is that the problem?