ocurl_example

ocurl is a binding to libcurl, a popular easy-to-use HTTP(S) client library. This library is useful to access to API servers or Web scraping.


In [1]:
#require "core" ;;
#require "curl" ;;
#require "uri" ;;
#require "yojson" ;;


/home/opam/.opam/4.04.1/lib/base/caml: added to search path
/home/opam/.opam/4.04.1/lib/base/caml/caml.cma: loaded
/home/opam/.opam/4.04.1/lib/base/shadow_stdlib: added to search path
/home/opam/.opam/4.04.1/lib/base/shadow_stdlib/shadow_stdlib.cma: loaded
/home/opam/.opam/4.04.1/lib/sexplib/0: added to search path
/home/opam/.opam/4.04.1/lib/sexplib/0/sexplib0.cma: loaded
/home/opam/.opam/4.04.1/lib/base: added to search path
/home/opam/.opam/4.04.1/lib/base/base.cma: loaded
/home/opam/.opam/4.04.1/lib/ocaml/unix.cma: loaded
/home/opam/.opam/4.04.1/lib/ocaml/bigarray.cma: loaded
/home/opam/.opam/4.04.1/lib/fieldslib: added to search path
/home/opam/.opam/4.04.1/lib/fieldslib/fieldslib.cma: loaded
/home/opam/.opam/4.04.1/lib/ppx_compare/runtime-lib: added to search path
/home/opam/.opam/4.04.1/lib/ppx_compare/runtime-lib/ppx_compare_lib.cma: loaded
/home/opam/.opam/4.04.1/lib/sexplib: added to search path
/home/opam/.opam/4.04.1/lib/sexplib/sexplib.cma: loaded
/home/opam/.opam/4.04.1/lib/variantslib: added to search path
/home/opam/.opam/4.04.1/lib/variantslib/variantslib.cma: loaded
/home/opam/.opam/4.04.1/lib/bin_prot/shape: added to search path
/home/opam/.opam/4.04.1/lib/bin_prot/shape/bin_shape_lib.cma: loaded
/home/opam/.opam/4.04.1/lib/bin_prot: added to search path
/home/opam/.opam/4.04.1/lib/bin_prot/bin_prot.cma: loaded
/home/opam/.opam/4.04.1/lib/ppx_hash/runtime-lib: added to search path
/home/opam/.opam/4.04.1/lib/ppx_hash/runtime-lib/ppx_hash_lib.cma: loaded
/home/opam/.opam/4.04.1/lib/ppx_inline_test/config: added to search path
/home/opam/.opam/4.04.1/lib/ppx_inline_test/config/inline_test_config.cma: loaded
/home/opam/.opam/4.04.1/lib/ppx_inline_test/runtime-lib: added to search path
/home/opam/.opam/4.04.1/lib/ppx_inline_test/runtime-lib/ppx_inline_test_lib.cma: loaded
/home/opam/.opam/4.04.1/lib/core_kernel/base_for_tests: added to search path
/home/opam/.opam/4.04.1/lib/core_kernel/base_for_tests/base_for_tests.cma: loaded
/home/opam/.opam/4.04.1/lib/jane-street-headers: added to search path
/home/opam/.opam/4.04.1/lib/jane-street-headers/jane_street_headers.cma: loaded
/home/opam/.opam/4.04.1/lib/ocaml/nums.cma: loaded
/home/opam/.opam/4.04.1/lib/num-top: added to search path
/home/opam/.opam/4.04.1/lib/num-top/num_top.cma: loaded
/home/opam/.opam/4.04.1/lib/num: added to search path
/home/opam/.opam/4.04.1/lib/ppx_assert/runtime-lib: added to search path
/home/opam/.opam/4.04.1/lib/ppx_assert/runtime-lib/ppx_assert_lib.cma: loaded
/home/opam/.opam/4.04.1/lib/ppx_bench/runtime-lib: added to search path
/home/opam/.opam/4.04.1/lib/ppx_bench/runtime-lib/ppx_bench_lib.cma: loaded
/home/opam/.opam/4.04.1/lib/ppx_expect/common: added to search path
/home/opam/.opam/4.04.1/lib/ppx_expect/common/expect_test_common.cma: loaded
/home/opam/.opam/4.04.1/lib/ppx_expect/config: added to search path
/home/opam/.opam/4.04.1/lib/ppx_expect/config/expect_test_config.cma: loaded
/home/opam/.opam/4.04.1/lib/ppx_expect/collector: added to search path
/home/opam/.opam/4.04.1/lib/ppx_expect/collector/expect_test_collector.cma: loaded
/home/opam/.opam/4.04.1/lib/stdio: added to search path
/home/opam/.opam/4.04.1/lib/stdio/stdio.cma: loaded
/home/opam/.opam/4.04.1/lib/typerep: added to search path
/home/opam/.opam/4.04.1/lib/typerep/typerep_lib.cma: loaded
/home/opam/.opam/4.04.1/lib/core_kernel: added to search path
/home/opam/.opam/4.04.1/lib/core_kernel/core_kernel.cma: loaded
/home/opam/.opam/4.04.1/lib/sexplib/unix: added to search path
/home/opam/.opam/4.04.1/lib/sexplib/unix/sexplib_unix.cma: loaded
/home/opam/.opam/4.04.1/lib/spawn: added to search path
/home/opam/.opam/4.04.1/lib/spawn/spawn.cma: loaded
/home/opam/.opam/4.04.1/lib/ocaml/threads: added to search path
/home/opam/.opam/4.04.1/lib/core: added to search path
/home/opam/.opam/4.04.1/lib/core/core.cma: loaded
/home/opam/.opam/4.04.1/lib/curl: added to search path
/home/opam/.opam/4.04.1/lib/curl/curl.cma: loaded
/home/opam/.opam/4.04.1/lib/bytes: added to search path
/home/opam/.opam/4.04.1/lib/re: added to search path
/home/opam/.opam/4.04.1/lib/re/re.cma: loaded
/home/opam/.opam/4.04.1/lib/re/re_posix.cma: loaded
/home/opam/.opam/4.04.1/lib/stringext: added to search path
/home/opam/.opam/4.04.1/lib/stringext/stringext.cma: loaded
/home/opam/.opam/4.04.1/lib/uri: added to search path
/home/opam/.opam/4.04.1/lib/uri/uri.cma: loaded
/home/opam/.opam/4.04.1/lib/easy-format: added to search path
/home/opam/.opam/4.04.1/lib/easy-format/easy_format.cmo: loaded
/home/opam/.opam/4.04.1/lib/biniou: added to search path
/home/opam/.opam/4.04.1/lib/biniou/biniou.cma: loaded
/home/opam/.opam/4.04.1/lib/yojson: added to search path
/home/opam/.opam/4.04.1/lib/yojson/yojson.cmo: loaded

In [2]:
open Core ;;

In [3]:
Curl.global_init Curl.CURLINIT_GLOBALALL ;;


Out[3]:
- : unit = ()

In [4]:
let search_query query =
  let b = Buffer.create 128 in
  let write_result s =
    Buffer.add_string b s ;
    String.length s
  in
  let base_uri = Uri.of_string "http://api.duckduckgo.com/?format=json" in
  let uri = Uri.add_query_param base_uri ("q", [query]) |> Uri.to_string in
  let c = Curl.init () in
  Curl.set_writefunction c write_result ;
  Curl.set_followlocation c true ;
  Curl.set_url c uri ;
  Curl.set_sslverifypeer c true ;
  Curl.perform c ;
  Curl.cleanup c ;
  let body = Buffer.contents b in
  let json = Yojson.Basic.from_string body in (* Parse a JSON *)
  (* Find field "Definition" or "Abstract" in a JSON *)
  let def = Yojson.Basic.Util.(json |> member "Definition" |> to_string_option) in
  let abs = Yojson.Basic.Util.(json |> member "Abstract" |> to_string_option) in
  match def with
  | None | Some "" -> abs
  | _ -> def


Out[4]:
val search_query : string -> string option = <fun>

In [5]:
search_query "ocaml"


Out[5]:
- : string option =
Some
 "OCaml, originally named Objective Caml, is the main implementation of the programming language Caml, created by Xavier Leroy, J\195\169r\195\180me Vouillon, Damien Doligez, Didier R\195\169my, Asc\195\161nder Su\195\161rez and others in 1996. A member of the ML language family, OCaml extends the core Caml language with object-oriented programming constructs."

In [ ]: