3

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);
halfer
  • 19,824
  • 17
  • 99
  • 186
JoelS
  • 89
  • 1
  • 1
  • 8
  • Could you not attach the GET parametre to the URL? yoururl.com?param=yourgetparam – Bolli Sep 02 '15 at 17:09
  • yes nothing gets attached to the URL – JoelS Sep 02 '15 at 17:10
  • Maybe you an use my answer here: http://stackoverflow.com/questions/13420952/php-curl-delete-request I have sent a lot of GET requests with that function. If you don't want to sent it in json, I think you can easily remove the json encode part and send your GET data as associated array. – Bolli Sep 02 '15 at 17:13
  • i have just tried that and still nothing gets added to the string.thanks for your answer – JoelS Sep 02 '15 at 17:19
  • Can you show me the code you use to construct your URL? – Bolli Sep 02 '15 at 17:44
  • the url i used is the http://www.php.net/search.php – JoelS Sep 02 '15 at 17:49
  • what does the GET parameters look like? – Bolli Sep 02 '15 at 18:02

1 Answers1

2

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.

Bolli
  • 4,974
  • 6
  • 31
  • 47
  • you should rather use `http_build_query()` and pass it an array instead of doing your GET query manually – Klemen Tusar Sep 02 '15 at 17:46
  • nope instead i get this: You must pass either an object or an array with the CURLOPT_HTTPHEADER – JoelS Sep 02 '15 at 17:47
  • @JoelS I updated the my answer. Does it work for you now? – Bolli Sep 02 '15 at 17:57
  • 1
    still nothing gets attached to the URL – JoelS Sep 02 '15 at 18:02
  • this did it: print("Example");, im not sure this is an official way of doing it though. – JoelS Sep 02 '15 at 18:11
  • 1
    That's a httpd configuration problem ... :P – Klemen Tusar Sep 02 '15 at 18:12
  • It 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. – Bolli Sep 02 '15 at 18:13
  • the get params were name.name.4 – JoelS Sep 02 '15 at 18:14
  • i got this when you told me to check for the get parameters being sent: Array ( [param] => datata [param2] => hekloworld ) – JoelS Sep 02 '15 at 18:16
  • thats a shame i really wanted to use cURL for this as well – JoelS Sep 02 '15 at 18:16
  • @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. – Bolli Sep 02 '15 at 18:22
  • is it possible to do the same thing but as a post/serverside-redirect? – JoelS Sep 02 '15 at 19:39
  • @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 Sep 02 '15 at 19:41
  • @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. – Bolli Sep 02 '15 at 19:42
  • yes 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? – JoelS Sep 02 '15 at 19:47
  • Im not completely sure what you mean? But you can send POST requests easily with cURL in PHP. Is this what you want? – Bolli Sep 02 '15 at 19:52
  • as in header() is a server side redirect, im just wondering whether you can do something very similar using cURL instead – JoelS Sep 02 '15 at 19:54
  • @JoelS Yes I think you need to search for 'php curl proxy' - I found this: http://stackoverflow.com/questions/5211887/how-to-use-curl-via-a-proxy and this: http://stackoverflow.com/questions/16934409/curl-as-proxy-deal-with-https-connect-method – Bolli Sep 02 '15 at 20:07
  • @Bolli can you show me how to do the equivalent via a POST method please? – JoelS Sep 03 '15 at 14:21
  • @JoelS I updated my post. This function works for both POST and get, simply change the $req parameter to what you need. – Bolli Sep 03 '15 at 14:34
  • @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. – Bolli Sep 03 '15 at 14:45
  • still nothing on the page – JoelS Sep 03 '15 at 14:49
  • @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 – Bolli Sep 03 '15 at 14:52
  • yes i did copy all of it and its displays on the same page not the test.php which is what im looking for. – JoelS Sep 03 '15 at 15:00
  • @JoelS that looks fine to me? Do you get blanc page? It should output the POST data you sent? – Bolli Sep 03 '15 at 15:56
  • yeh i got a blank page – JoelS Sep 03 '15 at 16:15
  • @JoelS when I copy paste your code I get the correct result. So thats strange! Do you have curl enabled on your localhost? – Bolli Sep 03 '15 at 16:20
  • yes. it worked with the get request, just the post is the problem – JoelS Sep 03 '15 at 16:23
  • @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? – Bolli Sep 03 '15 at 16:28
  • yes the location is correct im sure of that. thats very weird why is playing around like that. mmmm – JoelS Sep 03 '15 at 16:36
  • i dont get anything this is so frustrating – JoelS Sep 03 '15 at 16:46
  • @ 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"; } – Bolli Sep 03 '15 at 16:51
  • yes 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? – JoelS Sep 03 '15 at 17:08