...

View Full Version : Remove Resource ID from cURL post



Dubz
01-28-2012, 04:18 AM
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



function post2($url, $data){
$postCount = count(explode('&',$data));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $postCount);
curl_setopt($ch, CURLOPT_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.

jmj001
01-28-2012, 04:34 AM
try this one


function curl_post($url,$postData){
if(!isset($timeout))
$timeout=30;
$curl = curl_init();
if(isset($referer)){
curl_setopt ($curl, CURLOPT_REFERER, $referer);
}
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt ($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)');
curl_setopt ($curl, CURLOPT_HEADER, false);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt ($curl, CURLOPT_POST, true);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $postData);
curl_setopt ($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/x-www-form-urlencoded"));
$html = curl_exec ($curl);
curl_close ($curl);
return $html;
}

run it using


$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

Dubz
01-28-2012, 05:12 AM
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)



function post($url, $data, $ref = "") {
$url = parse_url($url);
$http = fsockopen($url['host'], 80, $en, $es, 45);
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($http, 128);
}
} else {
return array(
"status" => "error",
"error" => "(".$en.") ".$es
);
}
fclose($http);
$result = explode("\r\n\r\n", $result, 2);
$header = (isset($result[0])) ? $result[0] : false;
$content = (isset($result[1])) ? $result[1] : false;
return array(
"status" => "ok",
"header" => $header,
"content" => $content
);
}

tangoforce
01-28-2012, 01:53 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum