PHP/cURL Problem: Post method not working on webhost

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kevinng
Forum Newbie
Posts: 1
Joined: Tue Oct 21, 2008 11:20 am

PHP/cURL Problem: Post method not working on webhost

Post by kevinng »

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;
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP/cURL Problem: Post method not working on webhost

Post by requinix »

Code: Select all

/***********************************************************************
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;
    }
Reposted because apparently some people don't know what BBCode is.

All those

Code: Select all

if($method == HEAD)
Try putting the HEAD, GET, and POST in quotes. Like you're supposed to.

And put a

Code: Select all

ini_set("display_errors", 1);
at the top of your code (the one calling this function) and see what errors come up. Remove it when you're done, of course.
sunny12300
Forum Newbie
Posts: 14
Joined: Thu Jan 01, 2009 11:51 pm

Re: PHP/cURL Problem: Post method not working on webhost

Post by sunny12300 »

me having same problem.
i have inserted quote too but not working still.
and no error on script
Post Reply