Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-28-2012, 03:18 AM   PM User | #1
Dubz
Regular Coder

 
Join Date: Sep 2011
Posts: 206
Thanks: 15
Thanked 5 Times in 5 Posts
Dubz has a little shameless behaviour in the past
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.
Dubz is offline   Reply With Quote
Old 01-28-2012, 03:34 AM   PM User | #2
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
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
jmj001 is offline   Reply With Quote
Old 01-28-2012, 04:12 AM   PM User | #3
Dubz
Regular Coder

 
Join Date: Sep 2011
Posts: 206
Thanks: 15
Thanked 5 Times in 5 Posts
Dubz has a little shameless behaviour in the past
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
        
);
    } 
Dubz is offline   Reply With Quote
Old 01-28-2012, 12:53 PM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,667
Thanks: 46
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:36 AM.


Advertisement
Log in to turn off these ads.