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
 
You must always keep in mind that PHP does not report a parse error where the error exists. PHP instead reports the point in your script where it realized it was confused. With parse errors, the actual error occurs on a line before the line where the error is reported.

This is nearly certainly the case with your version of the script from my FAQ. Your line 27 reads:

$hostname = 'hostname.mysite.com

as such it is missing the terminal close quote and semicolon.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
thanks for replying sleep.

i did 4get those symbols when copying and pasting the script to this forum, but they are present in the script on my site.

Any idea what else it may be?

i wasnt sure if i should change

Code:
$hostname = 'hostname.mysite.com';
to
Code:
$hostname = 'mysite.com';

or not? i can find no possible reason for the error...
 
the exact code to more properly diagnose the problem:
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 (                            
                     
  'dbType' => 'mysql',
  'dbHost' => 'localhost',
  'dbUser' => 'memb',
  'dbPass' => 'okay',
  'dbName' => 'memb_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' => 'michaelldemery@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 ();

//    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;
 
How about this?
Code:
$data_string = ';
Should be:
Code:
$data_string = '';
Looking at the code, PHP would think all from this point on is string, and by the offending line the strings end and there is an out of place = sign.
 
verbatim1:
No one but you can answer the question of what hostname to use. It must be a name that your PHP-running machine can resolve to an IP address.

As to finding the bugs in your code, see section 1.3 of faq434-2999


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks Vragabond & sleipnir214 for the responses.

I did as you suggested Vragabond , i now get the error :

Parse error: parse error, unexpected T_STRING in /var/ on line 106

Knowing the error is above line 106, i now have to start at this line:

Code:
$result = curl_exec ($curl_handle) or die (There has been an error);

if anyone figures out the origin of the error please let me know.

Thanks again

and work my way up
 
The argument to the die() function needs to be a string. Literal strings are enclosed in quotes (single or double).

Please try to figure out the errors yourself before posting to the group.

Ken
 
you beat me to the post ken, i did figure that out. I appreciate you responding.

Hopefully someone will update the cURL tutorial so others dont have the same questions.
 
verbatim1:
I am the author of that FAQ, so I would be the one doing the updating. But interestingly, no one has clicked on the "Send a Comment to <authorname> About This FAQ" link that appears at the bottom of every FAQ in Tek-Tips.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks sleipnir214,

i sent you a comment.

Quick cURL question:

My initial thoughtwas to put the curl script in my html directory so i could just type and run the script.

However, I get a 403 forbidden error: You don't have permission to access /cgi-bin/ on this server using that method.

when i attempt to access the php file via my cgi-bin:
i get a 500 internal service error.

is it not possible for me to run the curl script from my web browser?

And what have i not configured [or mis-configured] giving me the above errors?
 
This sounds to me like a web server configuration problem.

Do other PHP scripts run on that system? What happened to the system on which you were running PHP and reporting errors?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Other php scripts do run on the system. This is the same system i was running php and reporting errors on.

This is the 1st script i have put on the system though that refers to the cgi-bin. I dont know if i need to make a index.html or .htaccess file inside the cgi-bin folder in order for this script to work correctly?

Initially i put the curl script in my html directory so i could just type and run the script. During my 1st attempt my cgi setting was

Code:
//    Path on that server to the CGI
$cgi = '[URL unfurl="true"]http://www.mysite.com/';[/URL]

that of course didnt work. I recieved the error:
Couldn't resolve host '
2nd attempt was
Code:
//    Path on that server to the CGI
$cgi = '/cgi-bin/';

error:

403 forbidden

Forbidden
You don't have permission to access /cgi-bin/ on this server.

my cgi path is /var/
what am i doing wrong?
 
You're having web-server problems. Since it looks like you are running Apache, I recommend you ask your questions in the Apache forum, forum65


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
sleipnir214,

What type of cgi script should this php cURL script be connecting to in order to successfully connect to a website and submit a form post?

I have realized that this is my error. I was putting the path to the php script in the $CGI path becasue i thought i would only need one file using the curl method to simulate a form post.

foggy, but becoming clearer it is...
 
i would like to post the info to a form on a php page, not cgi [which is where my confusion come into play.

i believe i should have the webpage address instead of the cgi parameters currently set up in the example.

the page is
how would i direct the script to post to the form?
 
I don't understand.

Do you mean that you want to write a script that fetches a form from a web server, parses it, figures out programmatically what should be posted where, then submits data to the script that is the target of that form?



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
yes, but i dont think my current setup will get it:
Code:
<?php
//    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 = '[URL unfurl="true"]www.mysite.com';[/URL]

//    Path on that server to the CGI
$cgi = '/cgi-bin/';

//    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 (                            
                     
  'dbType' => 'mysql',
  'dbHost' => 'localhost',
  'dbUser' => 'memb',
  'dbPass' => 'okay',
  'dbName' => 'memb_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 ();

//    Tell CURL the URL of the CGI
//curl_setopt ($curl_handle, CURLOPT_URL, $HTTP_method . '://' . $hostname . $cgi);
curl_setopt($curl_handle, CURLOPT_URL, "[URL unfurl="true"]http://www.mysite.com/userz/mel/sy_admin.php");[/URL]

//    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_VERBOSE, 1); 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 ("There has been an error");

echo curl_error($curl_handle);

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

//    Process the return
print $result;
 
I don't know if your current setup will or not as I have not looked at your code. Does your code fetch the page from the web server which hosts the page?

What you are trying to do is extraordinarily ambitious, particularly with your current PHP skill level.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
thanks guys for answering my questions in this post...


I have found the error of my ways:

In my eager attempt to configure cURL, I was listing the http address of the page where the form was located, and not the URL of the php script that was the 'form action'

once i noticed i was posting to the form and not the action i was able ot get a simple script on my site to work.

Not the form i need to work though.

I dont know where the action page is because the only thing there is a '?'.

Code:
<"form action="?" method="POST">

Is anyone familiar with this type of thing?

In the attempt to find where the variables are posted to, i have looked in other pages downloaded with the software for a list of the variables. No Luck, i only found one page that makes them into an array:

Code:
    $res = array();
    $res['database'] =
             array('title'       => INSTALL_CAT_DB,
                   'description' => INSTALL_CAT_DB_DESC,
                   'items' => array(array('var'         => 'dbType',
                                          'title'       => INSTALL_DBTYPE,
                                          'description' => INSTALL_DBTYPE_DESC,
                                          'type'        => 'list',
                                          'default'     => 'mysql',
                                          'flags'       => array('nosave', 'simpleInstall', 'probeDefault')),

                                    array('var'         => 'dbHost',
                                          'title'       => INSTALL_DBHOST,
                                          'description' => INSTALL_DBHOST_DESC,
                                          'type'        => 'string',
                                          'default'     => 'localhost',
                                          'flags'       => array('nosave', 'simpleInstall')),

                                    array('var'         => 'dbUser',
                                          'title'       => INSTALL_DBUSER,
                                          'description' => INSTALL_DBUSER_DESC,
                                          'type'        => 'string',
                                          'default'     => 'mem',
                                          'flags'       => array('nosave', 'simpleInstall')),

                                    array('var'         => 'dbPass',
                                          'title'       => INSTALL_DBPASS,
                                          'description' => INSTALL_DBPASS_DESC,
                                          'type'        => 'protected',
                                          'default'     => '',
                                          'flags'       => array('nosave', 'hideValue', 'simpleInstall')),

any idea where they variables are posted to so i can set the CURLOPT_URL ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top