Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    407
    Thanks
    18
    Thanked 26 Times in 26 Posts

    Question Remove Resource ID from cURL post

    I have the following cURL post function i just whipped up after googling around a little. I haven't used cURL before so please assist me in making the function better if you can :P

    PHP Code:
    function post2($url$data){
        
    $postCount count(explode('&',$data));
        
    $ch curl_init();
        
    curl_setopt($chCURLOPT_URL$url);
        
    curl_setopt($chCURLOPT_POST$postCount);
        
    curl_setopt($chCURLOPT_POSTFIELDS$data);
        
    curl_exec($ch);
        
    curl_close($ch);
        print(
    $ch);
        
    sleep(120);

    The problem I'm having is after echoing the $ch variable it says 'Resource id #something'. How can I get it to remove this without stripping the last so many characters (the number seems to vary)? Also, what is stored inside the $ch array? I tried print_r to it but theirs so much content it's hard to see.

  • #2
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    try this one
    PHP Code:
    function curl_post($url,$postData){
        if(!isset(
    $timeout))
            
    $timeout=30;
        
    $curl curl_init();
         if(isset(
    $referer)){
            
    curl_setopt ($curlCURLOPT_REFERER$referer);
        }
        
    curl_setopt ($curlCURLOPT_URL$url);
        
    curl_setopt ($curlCURLOPT_TIMEOUT$timeout);
        
    curl_setopt ($curlCURLOPT_USERAGENT'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)');
        
    curl_setopt ($curlCURLOPT_HEADERfalse);
        
    curl_setopt ($curlCURLOPT_RETURNTRANSFERtrue);
        
    curl_setopt ($curlCURLOPT_SSL_VERIFYPEERfalse);
        
    curl_setopt ($curlCURLOPT_SSL_VERIFYHOSTfalse);
        
    curl_setopt ($curlCURLOPT_POSTtrue);
        
    curl_setopt ($curlCURLOPT_POSTFIELDS$postData);
        
    curl_setopt ($curlCURLOPT_HTTPHEADER,
            array(
    "Content-type: application/x-www-form-urlencoded"));
        
    $html curl_exec ($curl);
        
    curl_close ($curl);
        return 
    $html;

    run it using

    PHP Code:
    $url "http://www.google.com";
    $postData "field1=abc&field2=xyz"
    echo curl_post($url,$postData); 
    i've hacked a couple of my specific setting out, hopefully it doesn't break it

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    407
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Thanks, the function you posted seems to work smoothly, so far. THe other thing I need is the original post function that I'm replacing returned an array set up with the header, content, and status. This is the post function below (to help add the other parts in if possible)

    PHP Code:
        function post($url$data$ref "") {
            
    $url parse_url($url);
            
    $http fsockopen($url['host'], 80$en$es45);
            if(
    $http) {
                
    fputs($http"POST ".$url['path']." HTTP/1.1\r\n");
                
    fputs($http"Host: ".$url['host']."\r\n");
                if(
    $ref != "") { fputs($http"Referer: ".$ref."\r\n"); }
                
    fputs($http"Content-type: application/x-www-form-urlencoded\r\n");
                
    fputs($http"Content-length: ".strlen($data)."\r\n");
                
    fputs($http"Connection: close\r\n\r\n");
                
    fputs($http$data);
                while(!
    feof($http)&&!is_bool($http)) {
                    @
    $result .= fgets($http128);
                }
            } else {
                return array(
                    
    "status" => "error",
                    
    "error" => "(".$en.") ".$es
                
    );
            }
            
    fclose($http);
            
    $result explode("\r\n\r\n"$result2);
            
    $header = (isset($result[0])) ? $result[0] : false;
            
    $content = (isset($result[1])) ? $result[1] : false;
            return array(
                
    "status" => "ok",
                
    "header" => $header,
                
    "content" => $content
            
    );
        } 

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,281
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    Quote Originally Posted by Dubz View Post
    The problem I'm having is after echoing the $ch variable it says 'Resource id #something'. How can I get it to remove this without stripping the last so many characters (the number seems to vary)? Also, what is stored inside the $ch array? I tried print_r to it but theirs so much content it's hard to see.
    Thats because $ch is a resource HANDLE. It isn't a string, array or any other item that can be printed to the screen. It's the same principle as a mysql resource.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •