Delete action from timeline - php

Im using below php code to post an item to the timeline:
$request_data=http_build_query(
array(
'access_token'=>'xxx',
'item'=>'url'
)
);
$c=curl_init('https://graph.facebook.com/me/zoo:action');
curl_setopt($c,CURLOPT_POST,true);
curl_setopt($c,CURLOPT_POSTFIELDS,$request_data);
curl_setopt($c,CURLOPT_RETURNTRANSFER,true);
$result=curl_exec($c);
$status=curl_getinfo($c,CURLINFO_HTTP_CODE);
curl_close($c);
Im now trying to delete an item, but cant get my head around what the corresponding curl code would be. Facebook says:
curl -X DELETE \
-F 'access_token=xxxx' \
'https://graph.facebook.com/{'{id_from_create_call}'}'
Where in the first block of code would I define the "-X" and "DELETE" arguments?...
Thanks for any pointers!...

You needs to send HTTP request with DELETE HTTP method instead of POST, which you define by curl_setopt($c,CURLOPT_POST,true); call. Look for CURLOPT_CUSTOMREQUEST option instead of CURLOPT_POST in the curl_setopt doc.
Just replace
curl_setopt($c,CURLOPT_POST,true);
with
curl_setopt($c,CURLOPT_CUSTOMREQUEST,"DELETE");
You can read another post on the SO for more details about custom requests.

Related

How to Post data from external HTML form to Eloqua Form using cURL

I want to post data from an external HTML form to a simple form created in Eloqua, using cURL method. I tried to follow this documentation but I am not able to post data to Eloqua.
When I try from command line (Windows) -
curl --user "usercreds" --header "Content-Type: application/json" --request POST --data "{"testfirstname":"abc","testlastname":"def","singleCheckbox":1}" https://secure.p0{POD Number}.eloqua.com/api/REST/1.0/data/form/{formid}
I get below error:
[{"type":"ObjectValidationError","property":"fieldValues","requirement":{"type":"NoDuplicatesRequirement"},"value":""}]
When I try from PHP, as mentioned here https://www.eehelp.com/question/using-curl-to-repost-to-eloqua-data/
or https://github.com/fredsakr/eloqua-php-request the curl returns HTTP code 0.
This is a simple form created in Eloqua without any validations.
I do not know what I am doing wrong here.

How to urlencode a empty array(not null but empty) in php, to pass to CURLOPT_POSTFIELDS of a curl request?

I am creating Fb messenger bot in php and using curl to interact with fb server. now for sending a file fb wants me to send request in this format
` curl \
-F recipient='{"id":"USER_ID"}' \
-F message='{"attachment":{"type":"image", "payload":{}}}' \
-F filedata=#/tmp/shirt.png;type=image/png \
"https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN" `
Here you can see payload is an empty array.
What I am doing is
$res['message'] = [
'attachment' => [
'type' => 'image',
'payload' => [
]
]
]; `
After this I am using curl_setopt like this
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_BINARYTRANSFER , true);
curl_setopt($process,CURLOPT_POSTFIELDS, http_build_query($data));
But this doesn't work and I am getting
{"error":{"message":"(#100) The parameter message[attachment] [payload] is required","type":"OAuthException","code":100,"fbtrace_id":"EaQ66DTssrV"}}
After some debugging I reached at the conclusion that since my attachment array is empty http_build_query does not take it into account.
see first comment here
So my question is how to urlencode a empty array in php.
what I have tried till now
tried using array() and ''(single quotes) by assigning it to payload(here payload was included but was empty and fb seerver returned same error)
I can send other type of message successfully that does not include empty array.
curl command given is correct as I executed it from my shell using ssh access.
content type is multipart/form data.
I am sending data and file in a single request is it fine?
tried passing data without encoding - fb error recipient is empty
tried passing array directly to POSTFIELDS but got {"error":{"message":"(#100) param recipient must be non-empty.","type":"OAuthException","code":100,"fbtrace_id":"BQ00BtZZmPF"}}
If there is a better way to execute this curl command in php please tell me. Thanks

Invalid API key as a reponse of PUT method using RESTServer codeigniter

I am using the codeigniter rest server api library.
When I enter http://localhost/RESTapi/api/question?X-API-KEY=XXX in Postman with the PUT method
I'm getting:
{
"status": false,
"error": "Invalid API key "
}
It works fine with GET method
How can I fix this issue?
I've seen some API's that do not look at the GET params if you make a POST or PUT request for credentials or are inconsistent in how they do it.
Really, credentials should go in headers either via the Authorize header or a custom one for many reasons like 'not logging credentials to access logs', but I digress.
In this case you can try:
Put (no pun) the X-API-KEY=XXX inside the body of the PUT just to see if this works
See if/how the library accepts the API key in a header
Looking at this library in particular (https://github.com/chriskacerguis/codeigniter-restserver), they do support the header X-API-KEY. This should be where you put the key for ALL requests--it's best practice not to pass them as url params.
Here's the commandline example using curl from their Github project.
curl -X POST -H "X-API-KEY: some_key_here" http://example.com/books
In PHP you can use curl to set header like this:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-API-KEY: XXX'));

How can I run this REST API call from PHP?

I'm currently building a project based on the Parse.com backend that includes uploading files.
Users can upload files and then access a list of these/download them, this all works fine.
However, I'm not sure how to implement the command to delete an upload. From the Parse.com forums as well as the Parse support document, the call is:
curl -X DELETE \
-H "X-Parse-Application-Id: <YOUR_APPLICATION_ID>" \
-H "X-Parse-Master-Key: <YOUR_MASTER_KEY>" \
https://api.parse.com/1/files/<FILE_NAME>
I've had a bit of a look online but the only curl commands I can find to execute commands is curl_setopt. I imagine the above needs to be converted, can anybody help with this or point me in the right direction?
So basically I need to be able to press a button on a website (through PHP) and have it run the above command.
Thanks in advance
According to given info you have to set custom request method 'DELETE' (by CURLOPT_CUSTOMREQUEST option) as well as custom headers (by CURLOPT_HTTPHEADER option).
So the code should look like this:
$options = array(
CURLOPT_NOBODY => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_HTTPHEADER => array(
'X-Parse-Application-Id: <YOUR_APPLICATION_ID>',
'X-Parse-Master-Key: <YOUR_MASTER_KEY',
),
CURLOPT_URL => 'https://api.parse.com/1/files/<FILE_NAME>',
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
echo $response;
If it's useful I made a simple class to handle the api calls request via Curl
https://github.com/niklongstone/php-api

Curl Requests in PHP - Using an API

I'm trying to figure out how to use the Cheddar API (http://cheddarapp.com/developer) in my PHP application.
Cheddar's API uses curl requests - which have been fine for me using in terminal but not in my index.php.
I'd like to create a button that when clicked, creates a task in a list call Colors. If a list does not exist, it'll create the list.
Have anybody used Cheddar's API or even included curl requests in PHP or even how to include them in Javascript which I'm guessing you use for things of this matter.
Update
Here's the Curl request for creating a task in Cheddar: https://cheddarapp.com/developer/tasks#create.
I'd like to make a button that onclick, it will create a task. Is it not as a simple as creating a function in Javascript and using onclick on an anchor?
I am using now days curl Php
$LOCAL_REST_URL = 'whateverurlofyourrestapi'
$json_part = { pass the data for post }
you will get your response in $buffer in json format iterate it use the response
As u said how to make a curl request i would like to give you a simple POST example
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$LOCAL_REST_URL);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,20);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS,$json_part);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
Where $json_partis the request body and $LOCAL_REST_URL is is your rest url
I am hoping this post will help you
You could run your terminal program with shell_exec from php.
Or transcript the curl-code to curl-requests from php, http://se2.php.net/manual/en/ref.curl.php

Categories