Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

the curl example FAQ434-2502 gave this error:

Status
Not open for further replies.

verbatim1

Programmer
Apr 18, 2005
58
US
i copied the curl example from FAQ434-2502 .

the only changes i made were to put in my data fields where the examples were.

changed
IP-resolvable FQDN of the server
$hostname = 'hostname.mysite.com

changed the CGI path

so my script looks like this:

Code:
<?php
/*
    There are two fundamental ways for PHP to send data to another
    CGI via the POST-method:  CURL and fopen.
    
    Where CURL is the easier of the two, fopen is more commonly available.
    
    Check the output of phpinfo() to see whether CURL is available on your system.
    
    
*/



/*
    This method uses CURL to contact the server.
    
*/


//    Either 'http' or 'https'. 'https' is only an option if OpenSSH
//    is available on your system.  Check phpinfo() to see whether
//    HTTPS is available.
$HTTP_method = 'http';

//    IP-resolvable FQDN of the server
$hostname = 'hostname.mysite.com

//    Path on that server to the CGI
$cgi = '/var/[URL unfurl="true"]www/cgi-bin';[/URL]

//    Array of data.  The foreach loop below is going to construct a field/data
//    string like the one you see in the URL of a GET-method CGI.
$my_data = array ( 

my data fields here
 );
//    This section constructs the field/value pairs of the form
//    field1=value1&field2=value2&field3=value3
$data_string = ';
$add_ampersand = FALSE;
foreach ($my_data as $key => $value)
{
    if ($add_ampersand)
    {
        $data_string .= '&';
    }
    $data_string .= $key . '=' . $value;
    $add_ampersand = TRUE;
}

//    Get a CURL handle
$curl_handle = curl_init ();

//    Tell CURL the URL of the CGI
curl_setopt ($curl_handle, CURLOPT_URL, $HTTP_method . '://' . $hostname . $cgi);

//    This section sets various options.  See [URL unfurl="true"]http://www.php.net/manual/en/function.curl-setopt.php[/URL]
//    for more details
curl_setopt ($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl_handle, CURLOPT_POST, 1);
curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $data_string);
    
//    Perform the POST and get the data returned by the server.
$result = curl_exec ($curl_handle) or die (&quot;There has been an error&quot;);

//    Close the CURL handle
curl_close ($curl_handle);

//    Process the return
print $result;

and i got the following error:

Parse error: parse error, unexpected '=' in /var/ on line 88

line 88 is :

Code:
$data_string .= $key . '=' . $value;

any idea why
 
I now know that the "?" means the php script action is back to the same script/page.

I would think this would solve my puzzle, it does not.

The puzzle: try to submit info to a form using cURL.

The puzzle variables:

1. the url of the form: www.mysite.com/userz/mel/sy_admin.php?sy[step]=2b

2. actual code of the page:

Code:
<?php
require_once 'sy/sy_admin.php';
?>

note: sy/sy_admin.php is in my home directory. /home/sy/sy_admin.php

3. critical code of sy/sy_admin.php : [i think, may not be important]
[code]
<?php
include('sy_config.inc.php');

if (IS_installed === false) {
    require_once(S9Y_INCLUDE_PATH . 'include/functions_installer.inc.php');
    require_once S9Y_INCLUDE_PATH . 'include/functions_config.inc.php';
    $css_file = 'sy.css.php?sy[css_mode]=sy_admin.css';
} else {
    $css_file = sy_rewriteURL('sy_admin.css');
}
<html>
    <head>
        <title><?php echo sy_ADMIN_SUITE; ?></title>
        <meta http-equiv="Content-Type" content="text/html; charset=<?php echo LANG_CHARSET; ?>" />
        <link rel="stylesheet" type="text/css" href="<?php echo $css_file; ?>" />
        <script type="text/javascript">
        function spawn() {
            if (self.Spawnextended) {
                Spawnextended();
            }

            if (self.Spawnbody) {
                Spawnbody();
            }

            if (self.Spawnnugget) {
                Spawnnugget();
            }
        }
        
        function SetCookie(name, value) {
            var today  = new Date();
            var expire = new Date();
            expire.setTime(today.getTime() + (60*60*24*30));
            document.cookie = 'sy[' + name + ']='+escape(value) + ';expires=' + expire.toGMTString();
        }
        </script>
    </head>
    <body id="sy_admin_page" onload="spawn()">   

<?php
if (!isset($sy['syPath']) || IS_installed === false || IS_up2date === false ) {
    if (IS_installed === false) {
        $file = 'include/admin/installer.inc.php';
    } elseif ( IS_up2date === false ) {
        $file = 'include/admin/upgrader.inc.php';
    } else {
        $file = ''; // For register_global, safety
    }
?>
?>

Now one of the functions in installer.inc.php is to print the form template.

This may all be irrelevant in what i am trying to do, if so please let me know.

My current curl attempt is:

Code:
<?php

 $my_data = array( 
'dbType' => 'mysql',
  'dbHost' => 'localhost',
  'dbUser' => 'mem',
  'dbPass' => 'letmein',
  'dbName' => 'mem_com_-_sy',
  'dbPrefix' => 'mel_',
  'dbPersistent' => 'false',
  'syPath' => '/var/[URL unfurl="true"]www/html/userz/mel/',[/URL]
  'uploadPath' => 'uploads/',
  'syHTTPPath' => '/userz/mel/',
  'templatePath' => 'templates/',
  'uploadHTTPPath' => 'uploads/',
  'baseURL' => '[URL unfurl="true"]http://www.mysite.com/userz/mel/',[/URL]
  'autodetect_baseURL' => 'false',  //dont know if true should have quotes around it it doesnt in config file
  'indexFile' => 'index.php',
  'user' => 'mel',
  'pass' => 'thanks',
  'realname' => 'mel',  //should get from signup p h p
  'email' => 'michael@hotmail.com',  //should get from signup p h p
  'want_mail' => 'true',  //dont know if true should have quotes around it it doesnt in config file
  'allowSubscriptions' => 'true',  //dont know if true should have quotes around it it doesnt in config file
  'blogTitle' => 'John Does personal blog',
  'blogDescription' => 'My little place on the web...',
  'lang' => '',  //dont know if true should have quotes around it it doesnt in config file
  'lang_content_negotiation' => 'false',  //dont know if true should have quotes around it it doesnt in config file
  'fetchLimit' => '15',  //dont know if true should have quotes around it it doesnt in config file
  'useGzip' => 'true',  //dont know if true should have quotes around it it doesnt in config file
  'wysiwyg' => 'false',  //dont know if true should have quotes around it it doesnt in config file
  'XHTML11' => 'false',  //dont know if true should have quotes around it it doesnt in config file
  'enablePopup' => 'false',  //dont know if true should have quotes around it it doesnt in config file
  'embed' => 'false',
  'top_as_links' => 'false',   //dont know if true should have quotes around it it doesnt in config file
  'blockReferer' => ',',
  'rewrite' => 'array()',  //dont know if true should have quotes around it it doesnt in config file
  'serverOffsetHours' => '0',  //dont know if true should have quotes around it it doesnt in config file
  'showFutureEntries' => 'false',  //dont know if true should have quotes around it it doesnt in config file
  'magick' => 'false',  //dont know if true should have quotes around it it doesnt in config file
  'convert' => '/usr/local/bin/convert',
  'thumbSuffix' => 'syThumb',
  'thumbSize' => '110',  //dont know if true should have quotes around it it doesnt in config file
                );
               
//    This section constructs the field/value pairs of the form
//    field1=value1&field2=value2&field3=value3
$data_string = '';
$add_ampersand = FALSE;
foreach ($my_data as $key => $value)
{
    if ($add_ampersand)
    {
              
        $data_string .= '&';
    }
    $data_string .= $key . '=' . $value;
    $add_ampersand = TRUE;
}

//    Get a CURL handle
$curl_handle = curl_init ();

curl_setopt($curl_handle, CURLOPT_URL, '[URL unfurl="true"]http://www.mysite.com/userz/mel/sy_admin.php?sy[/URL][step]=2b');

curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)');
curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_HEADER, 1);
$result=curl_exec ($curl_handle);
curl_close ($curl_handle);

preg_match_all("/Set\-Cookie\:.*?\;/", $result, $matches);
$cookiejar = "";
foreach ($matches[0] as $cookie) {
$cookiejar .= str_replace("Set-Cookie: ", "", $cookie).' ';
}

preg_match("/Location\:.*?\n/", $result, $matches);
$nexturl = str_replace("\r", "", str_replace("\n", "", str_replace("Location: ", "", $matches[0])));

// the 3 lines below were jst 4 test purposes to make sure the variables were getting set
print $cookie;    // ex result: Set-Cookie: PHPSESSID=dcd730a95f8b428f7dbd9fc1e6687d1a
print $nexturl;   // < got no result
print $cookiejar; // ex result PHPSESSID=dcd730a95f8b428f7dbd9fc1e6687d1a


//    Get a CURL handle
$curl_handle = curl_init ();
curl_setopt($curl_handle, CURLOPT_URL, $nexturl);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)');
curl_setopt($curl_handle, CURLOPT_REFERER, '[URL unfurl="true"]http://www.mysite.com/userz/mel/sy_admin.php?sy[/URL][step]=2b?');  
curl_setopt($curl_handle, CURLOPT_COOKIE, $cookiejar);
curl_setopt ($curl_handle, CURLOPT_POST, 1);
curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_HEADER, 1);
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 300);
$result=curl_exec ($curl_handle);
curl_close ($curl_handle);

//    Process the return
print $result;
?>

I notice that the initial form page is: [step]=2b
and after filling out the form the normal way
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top