M9A
M9A

Reputation: 3276

PHP Curl undefined variable error

Hi im attempting to login to a website using PHP and Curl. The problem I am having is that the names of the input fields on the website I want to log into have names that the script thinks is a variable. So when I run the script I get an error saying undefined variables.

$fields = "[email protected]&ctl00$MainContent$PasswordText=xxxx";

The errors I am getting are:

Notice: Undefined variable: MainContent 

Notice: Undefined variable: EmailText

Notice: Undefined variable: PasswordText

Is there any way around this?

Upvotes: 1

Views: 2892

Answers (3)

Rolf Wenger
Rolf Wenger

Reputation: 41

If you use ' instead of " of your variable-definition, php will not interpret the content inside. See: http://php.net/manual/en/language.types.string.php

Additionally, i always use the curl-option CURLOPT_POSTFIELDS for handling post-fields - because with this i can submit an array containing my values - that gives more beautiful code:

$curlhandle = curl_init();
$post_values = array(
    'ctl00$MainContent$EmailText' => '[email protected]'
    'ctl00$MainContent$PasswordText' => 'xxxx'
);
curl_setopt($curlhandle, CURLOPT_POST, true);
curl_setopt($curlhandle, CURLOPT_POSTFIELDS, $post_values);

Upvotes: 2

Menztrual
Menztrual

Reputation: 41597

Use single quotes:

$fields = '[email protected]&ctl00$MainContent$PasswordText=xxxx';

Upvotes: 3

Jon
Jon

Reputation: 437386

Yes, put the string inside single quotes instead of double.

Upvotes: 2

Related Questions