Ejemplo n.º 1
0
function micropub_post($endpoint, $access_token, $params, $photo_filename = false, $video_filename = false)
{
    $postfields = array('h' => 'entry', 'access_token' => $access_token);
    if (k($params, 'content')) {
        $postfields['content'] = $params['content'];
    }
    if (k($params, 'category')) {
        $postfields['category'] = $params['category'];
    }
    if (k($params, 'place_name')) {
        $postfields['place_name'] = $params['place_name'];
    }
    if (k($params, 'location')) {
        $postfields['location'] = $params['location'];
    }
    if (k($params, 'published')) {
        $postfields['published'] = $params['published'];
    }
    if (k($params, 'syndication')) {
        $postfields['syndication'] = $params['syndication'];
    }
    if (k($params, 'name')) {
        $postfields['name'] = $params['name'];
    }
    $multipart = new p3k\Multipart();
    $multipart->addArray($postfields);
    if ($photo_filename) {
        $multipart->addFile('photo', $photo_filename, 'image/jpeg');
    }
    if ($video_filename) {
        $multipart->addFile('video', $video_filename, 'video/mp4');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $endpoint);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $access_token, 'Content-Type: ' . $multipart->contentType()));
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $multipart->data());
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    $response = curl_exec($ch);
    $error = curl_error($ch);
    return array('response' => $response, 'error' => $error, 'curlinfo' => curl_getinfo($ch));
}
Ejemplo n.º 2
0
function micropub_post($endpoint, $params, $access_token, $file_path = NULL)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $endpoint);
    curl_setopt($ch, CURLOPT_POST, true);
    // Send the access token in both the header and post body to support more clients
    // https://github.com/aaronpk/Quill/issues/4
    // http://indiewebcamp.com/irc/2015-02-14#t1423955287064
    $httpheaders = array('Authorization: Bearer ' . $access_token);
    $params = array_merge(array('h' => 'entry', 'access_token' => $access_token), $params);
    if (!$file_path) {
        $post = http_build_query($params);
        $post = preg_replace('/%5B[0-9]+%5D/', '%5B%5D', $post);
        // change [0] to []
    } else {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mimetype = finfo_file($finfo, $file_path);
        $multipart = new p3k\Multipart();
        $multipart->addArray($params);
        $multipart->addFile('photo', $file_path, $mimetype);
        $post = $multipart->data();
        array_push($httpheaders, 'Content-Type: ' . $multipart->contentType());
    }
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    $response = curl_exec($ch);
    $error = curl_error($ch);
    $sent_headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);
    $request = $sent_headers . $post;
    return array('request' => $request, 'response' => $response, 'error' => $error, 'curlinfo' => curl_getinfo($ch));
}