Link to home
Start Free TrialLog in
Avatar of Cipy Ano
Cipy Ano

asked on

php curl - multipart/mixed issue

I am trying to send curl requests to google batches api, but i get an error (resource(2) of type curl).

Google Batch Documentation: https://developers.google.com/calendar/batch#batch-example-request

Here is my code:
function buildMixedRequest() {
    $CAL_BASE_URL  = 'https://www.googleapis.com/calendar/v3/calendars/';
    $CAL_BATCH_URL = 'https://www.googleapis.com/batch';        

    $ch = curl_init( $CAL_BATCH_URL );

    $events = array();
    $events[] = array( 'calendar_id' => 'primary', 'BookingRef' => '#1', 'StartDate' => '2018-06-6T09:05:00', 'EndDate' => '2018-06-6T11:05:00' );
    $events[] = array( 'calendar_id' => 'primary', 'BookingRef' => '#2', 'StartDate' => '2018-06-7T11:05:00', 'EndDate' => '2018-06-7T11:55:00' );  

    $calendar_id = 'primary';
    $access_token = '{my_token_here}';
    $boundary = 'batch_' . uniqid();
    $data = '';

    //$data .= "--" . $boundary . "\r\n";
    //$data .= 'Content-Type: application/http';

    // Add batches
    foreach ( $events as $event ) {
        $data .= "--" . $boundary . "\r\n"
            . 'PUT ' .  $CAL_BASE_URL . $calendar_id . '/events' . "\r\n"
            . 'Content-Type: application/json' . "\r\n"
            . 'Content-Length: ' . strlen(json_encode($event)) . "\r\n";
        $data .= json_encode($event) . "\r\n\r\n";
    }

    // Headers
    $headers = array(
        "Authorization: Bearer " .  $access_token,
        "Host: www.googleapis.com",
        "Content-Type: multipart/mixed; boundary={$boundary}",
        'Content-Length: ' . strlen(json_encode($events))
    );  

    // Curl Setup
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_POST, true);       
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);   
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);        
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    // Execute
    curl_exec($ch);

    // Get INFO
    $info = curl_getinfo($ch, CURLINFO_HEADER_OUT);

    print_r($info);
    print_r($data);

    // Make CURL reponse
    // $response = json_decode(curl_exec($ch));

    var_dump($ch);          

    // Close CURL
    curl_close($ch);        

    return $ch;
}

buildMixedRequest();

Open in new window

The curl info output is:
POST /batch HTTP/1.1
Host: www.googleapis.com
Accept: */*
Authorization: Bearer {my_token_here}
Content-Type: multipart/mixed; boundary=batch_5b18f5ea54b04
Content-Length: 217

--batch_5b18f5ea54b04
PUT https://www.googleapis.com/calendar/v3/calendars/primary/events
Content-Type: application/json
Content-Length: 107
{"calendar_id":"primary","BookingRef":"#1","StartDate":"2018-06-6T09:05:00","EndDate":"2018-06-6T11:05:00"}

--batch_5b18f5ea54b04
PUT https://www.googleapis.com/calendar/v3/calendars/primary/events
Content-Type: application/json
Content-Length: 107
{"calendar_id":"primary","BookingRef":"#2","StartDate":"2018-06-7T11:05:00","EndDate":"2018-06-7T11:55:00"}

Open in new window

What i am doing wrong? Can't see where the issue is.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial