2
    <?php
 namespace VEndor\Module\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
    {    protected $resultPageFactory;    public function __construct(
           \Magento\Framework\App\Action\Context $context,
           \Magento\Framework\View\Result\PageFactory $resultPageFactory)
       {
           $this->resultPageFactory = $resultPageFactory;
           parent::__construct($context);
       }    public function execute()    {                                $curl = curl_init();
                                    // Set some options - we are passing in a useragent too here
                                        curl_setopt_array($curl, array(
                                    CURLOPT_RETURNTRANSFER => 1,
                                    CURLOPT_URL => 'http://tgrestcURL.com/?item1=value&item2=value2',
                                    CURLOPT_USERAGENT => 'Codular Sample cURL Request'
                                ));
                                // Send the request & save response to $resp
                                $resp = curl_exec($curl);
                                // Close request to clear up some resources
                                curl_close($curl);
       }
    }
4
  • 1
    where this json data coming from to the controller.Just explain flow
    – vijay b
    Commented Mar 21, 2017 at 11:03
  • thanx for reply this json data coming from api Commented Mar 21, 2017 at 11:09
  • you are calling this API from controller right?using curl you can achieve it
    – vijay b
    Commented Mar 21, 2017 at 11:12
  • yes can you tell me please how i get this json body in controller in magento 2 Commented Mar 21, 2017 at 11:14

2 Answers 2

0

Once you've compiled PHP with cURL support, you can begin using the cURL functions. The basic idea behind the cURL functions is that you initialize a cURL session using the curl_init(), then you can set all your options for the transfer via the curl_setopt(), then you can execute the session with the curl_exec() and then you finish off your session using the curl_close(). Here is an example that uses the cURL functions to fetch the example.com homepage into a file

we can use CURL functionality in magento like we use in php.write below code in magento controller and set the required options in CURL as your API requires.

/ Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://testcURL.com/?item1=value&item2=value2',
    CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
4
  • great thanks for reply can yoyu tell me how i implement this code in controller Commented Mar 21, 2017 at 13:17
  • Controller is simple .PHP file .paste tis code in execute() mehod
    – vijay b
    Commented Mar 21, 2017 at 13:23
  • yes i did but not work for me Commented Mar 21, 2017 at 13:24
  • post your full controller code in question
    – vijay b
    Commented Mar 21, 2017 at 13:26
0

Inject json factory class:

\Magento\Framework\Controller\Result\JsonFactory to __construct function

On convert resp is to array from Json if $resp is json variable .

=====================================

<?php
namespace VEndor\Module\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;
    protected $jsonFactory;    
    public function __construct(
            \Magento\Framework\App\Action\Context $context, 
            \Magento\Framework\View\Result\PageFactory $resultPageFactory,
            \Magento\Framework\Controller\Result\JsonFactory $jsonFactory
            )
    {
        $this->resultPageFactory = $resultPageFactory;
        $this->jsonFactory = $jsonFactory;       
        parent::__construct($context);
    }
    public function execute()
    {
        $jsonFactory =  $this->jsonFactory->create();
        $curl = curl_init();
        // Set some options - we are passing in a useragent too here
        curl_setopt_array($curl, array(
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => 'http://tgrestcURL.com/?item1=value&item2=value2',
            CURLOPT_USERAGENT => 'Codular Sample cURL Request'
        ));
        // Send the request & save response to $resp
        $resp = curl_exec($curl);
        // Close request to clear up some resources
        curl_close($curl);
    }
    /* to Convert JSON to array */
    $resp = json_decode($resp);
    return $jsonFactory->setData($finalrespose);
}
1
  • it doesn't work it returns null. However if you remove the json_decode it return e.g "Array\n(\n [0] => Array\n (\n [country] => (not set)\n Commented Aug 14, 2018 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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