PHP/cURL Problem: Post method not working on webhost
Posted: Tue Oct 21, 2008 11:27 am
Hi,
I am having a problem with getting my script to post data on my webhost.
The code works perfectly on my local XAMPP. However, it does not work on my webhost.
On inspection, the post method is "POST" when I run it on XAMPP, but it will default to "GET" when I run it on my webhost. This results in rendering my "POST" variables unreadable.
Does anyone of you knows why?
I am using this function which I followed from a book:
/***********************************************************************
http($url, $ref, $method, $data_array, $incl_head)
-------------------------------------------------------------
DESCRIPTION:
This function returns a web page (HTML only) for a web page through
the execution of a simple HTTP GET request.
All HTTP redirects are automatically followed.
IT IS BEST TO USE ONE THE EARLIER DEFINED USER INTERFACES
FOR THIS FUNCTION
INPUTS:
$target Address of the target web site
$ref Address of the target web site's referrer
$method Defines request HTTP method; HEAD, GET or POST
$data_array A keyed array, containing query string
$incl_head TRUE = include http header
FALSE = don't include http header
RETURNS:
$return_array['FILE'] = Contents of fetched file, will also
include the HTTP header if requested
$return_array['STATUS'] = CURL generated status of transfer
$return_array['ERROR'] = CURL generated error status
***********************************************************************/
function http($target, $ref, $method, $data_array, $incl_head)
{
# Initialize PHP/CURL handle
$ch = curl_init();
# Prcess data, if presented
if(is_array($data_array))
{
# Convert data array into a query string (ie animal=dog&sport=baseball)
foreach ($data_array as $key => $value)
{
if(strlen(trim($value))>0)
$temp_string[] = $key . "=" . urlencode($value);
else
$temp_string[] = $key;
}
$query_string = join('&', $temp_string);
}
# HEAD method configuration
if($method == HEAD)
{
curl_setopt($ch, CURLOPT_HEADER, TRUE); // No http head
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // Return body
}
else
{
# GET method configuration
if($method == GET)
{
if(isset($query_string))
$target = $target . "?" . $query_string;
curl_setopt ($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt ($ch, CURLOPT_POST, FALSE);
}
# POST method configuration
else if($method == POST)
{
if(isset($query_string))
curl_setopt ($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt ($ch, CURLOPT_HTTPGET, FALSE);
curl_setopt ($ch, CURLOPT_POST, TRUE);
}
curl_setopt($ch, CURLOPT_HEADER, $incl_head); // Include head as needed
curl_setopt($ch, CURLOPT_NOBODY, FALSE); // Return body
}
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE); // Cookie management.
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
curl_setopt($ch, CURLOPT_TIMEOUT, CURL_TIMEOUT); // Timeout
curl_setopt($ch, CURLOPT_USERAGENT, WEBBOT_NAME); // Webbot name
curl_setopt($ch, CURLOPT_URL, $target); // Target site
curl_setopt($ch, CURLOPT_REFERER, $ref); // Referer value
curl_setopt($ch, CURLOPT_VERBOSE, FALSE); // Minimize logs
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // No certificate
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects
curl_setopt($ch, CURLOPT_MAXREDIRS, 4); // Limit redirections to four
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Return in string
# Create return array
$return_array['FILE'] = curl_exec($ch);
$return_array['STATUS'] = curl_getinfo($ch);
$return_array['ERROR'] = curl_error($ch);
# Close PHP/CURL handle
curl_close($ch);
# Return results
return $return_array;
}
I am having a problem with getting my script to post data on my webhost.
The code works perfectly on my local XAMPP. However, it does not work on my webhost.
On inspection, the post method is "POST" when I run it on XAMPP, but it will default to "GET" when I run it on my webhost. This results in rendering my "POST" variables unreadable.
Does anyone of you knows why?
I am using this function which I followed from a book:
/***********************************************************************
http($url, $ref, $method, $data_array, $incl_head)
-------------------------------------------------------------
DESCRIPTION:
This function returns a web page (HTML only) for a web page through
the execution of a simple HTTP GET request.
All HTTP redirects are automatically followed.
IT IS BEST TO USE ONE THE EARLIER DEFINED USER INTERFACES
FOR THIS FUNCTION
INPUTS:
$target Address of the target web site
$ref Address of the target web site's referrer
$method Defines request HTTP method; HEAD, GET or POST
$data_array A keyed array, containing query string
$incl_head TRUE = include http header
FALSE = don't include http header
RETURNS:
$return_array['FILE'] = Contents of fetched file, will also
include the HTTP header if requested
$return_array['STATUS'] = CURL generated status of transfer
$return_array['ERROR'] = CURL generated error status
***********************************************************************/
function http($target, $ref, $method, $data_array, $incl_head)
{
# Initialize PHP/CURL handle
$ch = curl_init();
# Prcess data, if presented
if(is_array($data_array))
{
# Convert data array into a query string (ie animal=dog&sport=baseball)
foreach ($data_array as $key => $value)
{
if(strlen(trim($value))>0)
$temp_string[] = $key . "=" . urlencode($value);
else
$temp_string[] = $key;
}
$query_string = join('&', $temp_string);
}
# HEAD method configuration
if($method == HEAD)
{
curl_setopt($ch, CURLOPT_HEADER, TRUE); // No http head
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // Return body
}
else
{
# GET method configuration
if($method == GET)
{
if(isset($query_string))
$target = $target . "?" . $query_string;
curl_setopt ($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt ($ch, CURLOPT_POST, FALSE);
}
# POST method configuration
else if($method == POST)
{
if(isset($query_string))
curl_setopt ($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt ($ch, CURLOPT_HTTPGET, FALSE);
curl_setopt ($ch, CURLOPT_POST, TRUE);
}
curl_setopt($ch, CURLOPT_HEADER, $incl_head); // Include head as needed
curl_setopt($ch, CURLOPT_NOBODY, FALSE); // Return body
}
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE); // Cookie management.
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
curl_setopt($ch, CURLOPT_TIMEOUT, CURL_TIMEOUT); // Timeout
curl_setopt($ch, CURLOPT_USERAGENT, WEBBOT_NAME); // Webbot name
curl_setopt($ch, CURLOPT_URL, $target); // Target site
curl_setopt($ch, CURLOPT_REFERER, $ref); // Referer value
curl_setopt($ch, CURLOPT_VERBOSE, FALSE); // Minimize logs
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // No certificate
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects
curl_setopt($ch, CURLOPT_MAXREDIRS, 4); // Limit redirections to four
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Return in string
# Create return array
$return_array['FILE'] = curl_exec($ch);
$return_array['STATUS'] = curl_getinfo($ch);
$return_array['ERROR'] = curl_error($ch);
# Close PHP/CURL handle
curl_close($ch);
# Return results
return $return_array;
}