Results 1 to 2 of 2

Thread: Need Help with PHP CURL

  1. #1
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

    Default Need Help with PHP CURL

    Hi,

    Anyone here familiar with working with REST based API's and PHP's CURL library..?
    I'm trying to do a PU request but having some issues, my code is below:

    function CallCURL($method, $url, $data = false){
    $curl = curl_init();
    switch ($method){
    case "POST":
    curl_setopt($curl, CURLOPT_POST, 1);

    if ($data)
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    break;
    case "PUT":
    curl_setopt($curl, CURLOPT_PUT, 1);
    break;
    default:
    if ($data)
    $url = sprintf("%s?%s", $url, http_build_query($data));
    }

    // Optional Authentication:
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, "usernameassword");

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //setting content type and Accept JSON headers
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json'));

    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
    }

    //Call function
    print_r(CallAPI("PUT", "http://some-api-url/v1/books", $data));

  2. #2
    Join Date
    Mar 2011
    Posts
    485
    Rep Power
    0

    Default ... my code is below:

    Quote Originally Posted by Desmond View Post
    Hi,

    Anyone here familiar with working with REST based API's and PHP's CURL library..?
    I'm trying to do a PU request but having some issues, my code is below:

    function CallCURL($method, $url, $data = false){
    $curl = curl_init();
    ...
    print_r(CallAPI("PUT", "http://some-api-url/v1/books", $data));
    Lazy dev here. Plz post yer error log.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •