0

Is it possible to call my controller action from a helper?

I have a controller action in app\code\local\Fekete\Newsletter2Go\controllers\IndexController.php

public function apiAction()
{
    echo "hello";

}

This is a snippet of my helper app\code\local\Fekete\Newsletter2Go\Helper\Data.php. I try to call the controller action apiAction().

public function getIsSubscribedAtNewsletter2Go($email)
{
    $url = '/fekete_newsletter2go/index/api';

    $data = array(
        'email' => $email,
    );

    // use key 'http' even if you send the request to https://...
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data)
        )
    );

    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    if ($result === FALSE) { return "fail!"; }

    return "success";

}

But this always returns "fail!".

What am I missing?

0

I solved it. My dev site is protected with directory protection...

enter image description here

...so I had to change it to this:

$url = Mage::getBaseUrl() .'/fekete_newsletter2go/index/api';

$data = array(
    'email' => $email,
);

$auth = base64_encode("ourUser:ourPassword");

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => 'Authorization: Basic '. $auth,
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.