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.

Share:
11,618
JoelS
Author by

JoelS

Updated on June 04, 2022

Comments

  • JoelS
    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
    Klemen Tusar over 8 years
    you should rather use http_build_query() and pass it an array instead of doing your GET query manually
  • JoelS
    JoelS over 8 years
    nope instead i get this: You must pass either an object or an array with the CURLOPT_HTTPHEADER
  • Bolli
    Bolli over 8 years
    @JoelS I updated the my answer. Does it work for you now?
  • JoelS
    JoelS over 8 years
    still nothing gets attached to the URL
  • JoelS
    JoelS over 8 years
    this did it: print("<a href=\"Profile.php?$params\">Example</a>");, im not sure this is an official way of doing it though.
  • Klemen Tusar
    Klemen Tusar over 8 years
    That's a httpd configuration problem ... :P
  • Bolli
    Bolli over 8 years
    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.
  • JoelS
    JoelS over 8 years
    the get params were name.name.4
  • JoelS
    JoelS over 8 years
    i got this when you told me to check for the get parameters being sent: Array ( [param] => datata [param2] => hekloworld )
  • JoelS
    JoelS over 8 years
    thats a shame i really wanted to use cURL for this as well
  • Bolli
    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
    JoelS over 8 years
    is it possible to do the same thing but as a post/serverside-redirect?
  • Bolli
    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
    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
    JoelS over 8 years
    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?
  • Bolli
    Bolli over 8 years
    Im not completely sure what you mean? But you can send POST requests easily with cURL in PHP. Is this what you want?
  • JoelS
    JoelS over 8 years
    as in header() is a server side redirect, im just wondering whether you can do something very similar using cURL instead
  • Bolli
    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-pr‌​oxy and this: stackoverflow.com/questions/16934409/…
  • JoelS
    JoelS over 8 years
    @Bolli can you show me how to do the equivalent via a POST method please?
  • Bolli
    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
    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
    JoelS over 8 years
    still nothing on the page
  • Bolli
    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
    JoelS over 8 years
    yes i did copy all of it and its displays on the same page not the test.php which is what im looking for.
  • Bolli
    Bolli over 8 years
    @JoelS that looks fine to me? Do you get blanc page? It should output the POST data you sent?
  • JoelS
    JoelS over 8 years
    yeh i got a blank page
  • Bolli
    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
    JoelS over 8 years
    yes. it worked with the get request, just the post is the problem
  • Bolli
    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
    JoelS over 8 years
    yes the location is correct im sure of that. thats very weird why is playing around like that. mmmm
  • JoelS
    JoelS over 8 years
    i dont get anything this is so frustrating
  • Bolli
    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
    JoelS over 8 years
    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?