Skip to content

How to use curl to communicate with magento api

I am trying to post some data to the magento api using curl and then get a response. I have searched around a bit and made a short code snippit that should do the job but it returns a blank string. I should receive a oauth_token from magento or at least an error message.

The code below is what i came up with:

function file_get_contents_curl($url, $test, $data) {

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $result = curl_exec($ch);
    curl_close($ch);

    return $result;
}

$url contains the url an $data contains the data that needs to be sent.

the code bellow is the line that triggers this function

$response = file_get_contents_curl($endpoint->getAbsoluteUri(), false, $context);

If anyone sees the problem please let me know!