I'm going crazy trying to figure out whats happening to an easy piece of code.
I'll write less code as I can to focus the issue.
Target
Execute two or more cURL requests with the same handle but different options.
cURL wrapper portion
public function __construct(){
$this->handle = curl_init();
}
public function execute(){
$handle = $this->handle;
# ... some default, not relevant, options ... #
# Retrieve the http method
$http_method = $this->http_method;
# Set the method
switch($http_method){
case 'GET':
curl_setopt($handle, CURLOPT_HTTPGET, true);
break;
case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
break;
default:
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, $http_method);
break;
}
# Set the url
curl_setopt($handle, CURLOPT_URL, $this->url);
# Retrieve the payload
$payload = $this->payload;
# Set the payload
if(!empty($payload)){
# Specify data to send
curl_setopt($handle, CURLOPT_POSTFIELDS, $payload);
}
# Execute the request
$result = curl_exec($handle);
# Reset the handle
curl_reset($handle);
# Update the handle
$this->handle = $handle;
}
Sample of use
# First
$obj->setHttpMethod('post');
$obj->setURL("someurl");
$payload = array(
'field' => 'value'
);
$obj->setPayload($payload);
$result = $obj->execute();
# Second
$obj->setHttpMethod('get');
$obj->setURL("anotherurl");
$result = $obj->execute();
Result
Second is executed as a POST instead of GET.
Conclusion
Thanks in advance: I will love any suggestion.