// Aqui el metodo que hace el "Push"
function Push(Url, APIKey, DeviceID, Title, ContentType, Content: AnsiString): String;
var
Curl: TCURL;
Response: TStringStream;
HttpPost, Last: pcurl_httppost;
ContentName: AnsiString;
begin
Result:= EmptyStr;
Curl:= curl_easy_init;
if Curl <> nil then
try
if (curl_easy_setopt(Curl, CURLOPT_VERBOSE, FALSE) <> CURLE_OK) or
(curl_easy_setopt(Curl, CURLOPT_USE_SSL, CURLUSESSL_ALL) <> CURLE_OK) or
(curl_easy_setopt(Curl, CURLOPT_SSL_VERIFYPEER, FALSE) <> CURLE_OK) or
(curl_easy_setopt(Curl, CURLOPT_URL, PAnsiChar(URL)) <> CURLE_OK) or
(curl_easy_setopt(Curl, CURLOPT_USERPWD, PAnsiChar(APIKey+':')) <> CURLE_OK) or
(curl_easy_setopt(Curl, CURLOPT_WRITEFUNCTION, @CurlSaveToStream) <> CURLE_OK) then
Exit;
HttpPost:= nil;
Last:= nil;
try
if (curl_formadd(@HttpPost, @Last, CURLFORM_COPYNAME, PAnsiChar('device_id'),
CURLFORM_COPYCONTENTS, PAnsiChar(DeviceId), CURLFORM_END) <> CURL_FORMADD_OK) then
Exit;
if (Title <> EmptyStr) and (curl_formadd(@HttpPost, @Last, CURLFORM_COPYNAME, PAnsiChar('title'),
CURLFORM_COPYCONTENTS, PAnsiChar(Title), CURLFORM_END) <> CURL_FORMADD_OK) then
Exit;
if (curl_formadd(@HttpPost, @Last, CURLFORM_COPYNAME, PAnsiChar('type'),
CURLFORM_COPYCONTENTS, PAnsiChar(ContentType), CURLFORM_END) <> CURL_FORMADD_OK) then
Exit;
ContentName:= 'body';
if ContentType = 'note' then
ContentName:= 'body'
else if ContentType = 'url' then
ContentName:= 'link'
else if ContentType = 'address' then
ContentName:= 'address'
else if ContentType = 'file' then
ContentName:= 'file';
if ContentType = 'file' then
begin
if (curl_formadd(@HttpPost, @Last, CURLFORM_COPYNAME, PAnsiChar('file'),
CURLFORM_FILE, PAnsiChar(Content), CURLFORM_END) <> CURL_FORMADD_OK) then
Exit;
end else
if (curl_formadd(@HttpPost, @Last, CURLFORM_COPYNAME, PAnsiChar(ContentName),
CURLFORM_COPYCONTENTS, PAnsiChar(Content), CURLFORM_END) <> CURL_FORMADD_OK) then
Exit;
Response:= TStringStream.Create(EmptyStr);
try
if (curl_easy_setopt(Curl, CURLOPT_FILE, Pointer(Response)) <> CURLE_OK) or
(curl_easy_setopt(Curl, CURLOPT_HTTPPOST, HttpPost) <> CURLE_OK) then
Exit;
if curl_easy_perform(Curl) = CURLE_OK then
Result:= Response.DataString;
finally
Response.Free;
end;
finally
if HttpPost <> nil then
curl_formfree(HttpPost);
end;
finally
curl_easy_cleanup(Curl);
end;
end;
// Que se llama de esta manera
procedure TfrmMain.btnPushClick(Sender: TObject);
var
DeviceId: String;
Title: String;
Body: String;
begin
if (cmbDevices.ItemIndex >= 0) then
begin
DeviceId:= TDeviceItem(cmbDevices.Items.Objects[cmbDevices.ItemIndex]).Id;
Title:= Trim(txtTitle.Text);
Body:= Trim(txtBody.Text);
if (DeviceId <> EmptyStr) and (Title <> EmptyStr) and (Body <> EmptyStr) then
CheckResponse(Push('https://api.pushbullet.com/api/pushes', strAPIKey, DeviceId,
Title, 'note', Body));
end;
end;