We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Can I create an associative array in a plugin?

Development and Programming

Kevin Smith's avatar
Kevin Smith
4,784 posts
15 years ago
Kevin Smith's avatar Kevin Smith

So I’m relatively new to PHP, and definitely new to OOP. I’d love some help here!

I’m trying to use CURL to retrieve an XML document from another piece of software running on the server, and I think a plugin will be sufficient for this. I’m using the CURLOPT_POSTFIELDS option below to include the necessary information for authentication, etc.

curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);

$postfields is an array, and in a normal PHP doc, I’d just include this:

$postfields["username"] = "XXXXXXXX"; // API username
$postfields["password"] = "XXXXXXXX"; // API password
$postfields["responsetype"] = "xml";

However, it doesn’t look like I can create an associative array inside the plugin’s class, can I? Could anyone shed some light on this?

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
15 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

Sure you can use an associative array in the class. What does your plugin look like so far?

       
Kevin Smith's avatar
Kevin Smith
4,784 posts
15 years ago
Kevin Smith's avatar Kevin Smith

Well I found this on Stack Overflow, but I’m not sure it’s what I need to be doing. Is it?

My plugin, at this moment, is pretty much scrambled bits of code. Basically, I need to set the variables (seen in the second code block above) so that they can be used as an array in the CURLOPT_POSTFIELDS option (seen in the first block of code).

       
Kevin Smith's avatar
Kevin Smith
4,784 posts
15 years ago
Kevin Smith's avatar Kevin Smith

Ok, wait a second. Is it really just as easy as this?

var $postfields = array(
                            'username'=>'XXXXX',    // Admin username
                            'password'=>'md5("XXXXXX")',    // Admin password
                            'responsetype'=>'xml'
                            );
       
Kevin Smith's avatar
Kevin Smith
4,784 posts
15 years ago
Kevin Smith's avatar Kevin Smith

Nope, it’s borking on the md5 part.

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
15 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team
Nope, it’s borking on the md5 part.

That’s because you’ve wrapped the PHP function in quotes.

var $postfields = array(
                            'username'=>'XXXXX',    // Admin username
                            'password'=>md5("XXXXXX"),    // Admin password
                            'responsetype'=>'xml'
                            );
       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
15 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team
Ok, wait a second. Is it really just as easy as this?

And yes, it really is that easy to use an array inside a Class. If you want to post your plugin code it might be easier to make comments or suggestions in context 😊

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.