HTTPS via Curl JSON response

Asked

Viewed 1,916 times

1

  • curl https://api.mercadolibre.com/sites/MLA/search/?

  • By the php tag, it must be a method to perform a request using the Curl API.

1 answer

4


<?php
//$url = 'https://api.mercadolibre.com/sites/MLB/search/';
$url = 'https://api.mercadolibre.com/sites/MLB/search?category=MLB1648';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CAINFO, getcwd() . DIRECTORY_SEPARATOR . 'cacert.pem');

$output = curl_exec($curl);
curl_close($curl);

header('Content-Type: application/json');
echo $output;

The archive cacert.pem must be downloaded at http://curl.haxx.se/ca/cacert.pem and placed in the same folder where the above script is.

NOTE: When using the Free Market API, specify MLB to search for data in the Brazilian version of the site. MLA refers to the Argentine version.


It is also possible to create a function to facilitate a little:

<?php

function get($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_CAINFO, getcwd() . DIRECTORY_SEPARATOR . 'cacert.pem');

    $output = curl_exec($curl);
    curl_close($curl);

    return $output;
}

header('Content-Type: application/json');

// Obtém produtos da categoria "Informática"
echo get('https://api.mercadolibre.com/sites/MLB/search?category=MLB1648');

// Obtém as categorias do site;
echo get('https://api.mercadolibre.com/sites/MLB/categories');

// Faz uma busca com a palavra-chave "pendrive"
echo get('https://api.mercadolibre.com/sites/MLB/search?q=pendrive');

See also:

  • 1

    Gabriel, it worked with this certificate. Thank you very much for your help. Vlw.

  • 1

    You are welcome. We are here to teach and learn from each other.

  • another option, which defines the SSL version: curl_setopt($Curl, CURLOPT_SLVERSION, Curl_sslversion_tlsv1);

  • Is this cacert.pem file safe?? And what hacker site is this http://curl.haxx.se ? Sincerely take great care, the right is the PEM file to be generated on the server itself, one for each domain!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.