0

Here is my index.php file code Path : /var/www/html/m243/app/code/Vendor/Module/Controller/Index/Index.php

 public function execute()
    {
       
        $url = $this->urlBuilder->getUrl(
            'wpsharecart/index/viewcart', ['id' => $this->urlEncoder->encode($this->session->getQuoteId())]
        );
        $siteurl = $this->urlBuilder->getUrl();


        /*Bitly URL Convert Code START*/
        
            $long_url = $url;
            $apiv4 = 'https://api-ssl.bitly.com/v4/shorten';
            $genericAccessToken = 'xyzxyyzabcabcbcbcbcbcbcbccccc';

            $data = array(
                'long_url' => $long_url
             );
            $payload = json_encode($data);

            $header = array(
                'Authorization: Bearer ' . $genericAccessToken,
                'Content-Type: application/json',
                'Content-Length: ' . strlen($payload)
            );

            $ch = curl_init($apiv4);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            $result = curl_exec($ch);
            $resultToJson = json_decode($result);
            //echo $resultJson->link;           

        /*Bitly URL Convert Code END*/

        $resultToJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
        $resultToJson->setData($url);
        return $resultToJson;
        $data = $this->getRequest()->getPostValue();
        //return $this->_pageFactory->create();
    }

I want to print $resultToJson = json_decode($result); data,

I tried many codes but it's not working like

echo $resultToJson

print_r($resultToJson)

no, any solution worked for me. Can anyone please help me with this?

0

The problem here is that you are overriding the $resultToJson which contains your data..

try renaming this: $resultToJson = json_decode($result); in $data = json_decode($result);

then:

$resultToJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$resultToJson->setData($data);
return $resultToJson;

More info here: https://meetanshi.com/blog/return-json-response-from-controller-in-magento-2/

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.