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
Related
I have searched a lot, and found many related questions and forums related to this, but this one is a challenging one.
I'm trying to POST a complex array via curl. It has to be form-data while the first value in the array is of type JSON.
The two other values of array are two images which are uploaded and ready to send.
I tried to run it in Postman, and works perfectly fine. I used the generated PHP code from Postman, but it is not working. Seems like postman is handling some of its tricks without revealing them to us.
Any way, I'm posting a Postman image to illustrate what I mean:
As you can see, I'm sending the data in form-data tab, my first value (param1) is a JSON with content-type application/json, the second and third values are images uploaded in Postman.
This works just fine in Postman.
The problem is, if I set Content-Type:multipart/form-data in header, the destination server throws an error saying the content-type must be JSON.
If I set the Content-Type:application/json in header, the destination server says content must be of type Multipart.
Somehow, I need to set both content-types. The main one as form-data and the one for param1 as JSON.
I paste the Postman code as well, may that be a good start for you fellas to help out with the code.
Postman Code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://xxxxx.com/xxxx/xxx/xxxx',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('param1' => '{
"AgentId":"1414",
"ContractId":36529,
"Files":[
{
"FileName":"car_card_front_image.png",
"FileTypeId":2
},
{
"FileName":"car_card_back_image.png",
"FileTypeId":2
}
]
}','param2'=> new CURLFILE('/C:/images/icons/car_card_back_image.png'),'param3'=> new CURLFILE('/C:/images/icons/car_card_front_image.png')),
CURLOPT_HTTPHEADER => array(
'authenticationToken: xxxx-xxx-xx-xxxxxxxx'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
The PHP generated code by postman, is not working. One of the reasons can be that there's no content-type mentioned in it.
I tried modifying the code, adding content-types in header and in parameter, nothing seems to work.
If Postman can do it, we should be able it too, right?
Go ahead, make as much changes as you would or suggest anything that comes to your mind, I will test them all.
Cheeeeers...
May i suggest the ixudrra/curl library ?
It would make your life easier ....
$response = Curl::to('http://example.org')
->withData( array( 'Foo' => 'Bar' ) )
->withFile( 'image_1', '/path/to/dir/image1.png', 'image/png', 'imageName1.png' )
->withFile( 'image_2', '/path/to/dir/image2.png', 'image/png', 'imageName2.png' )
->post();
TLDR: I want to use get_theme_mod() inside a PHP file other than functions.php in WordPress to use a value added through customizer.
Long version:
I am making an API call using cURL in a separate PHP file. I want to create a new panel in customizer with an inputfield that accepts the API-key for my custom PHP API call. Basically I need to know if I can use the get_theme_mod() method to call the value into my custom PHP file, store it in a variable and use it to append it to my API call.
This is my API call code
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://maps.googleapis.com/maps/api/place/details/json?placeid=< GOOGLE PLACE ID >&key=< MY API KEY>",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Once the call is made, I AJAX this file into a JavaScript file where I work with the data. Which is initially why I put the API call into a separate file. My goal is for the person working on the site to simply paste an API key, or the place id for Google and have the entire process execute without having to monkey around in the source code. I have all the infrastructure in place and the call works I just wanted to eliminate the need for us to touch the source code and simply drop an API key value in the customizer and let the code do the rest.
I'm assuming you mean the theme customizer? Maybe instead save settings using the Settings API. But you should also do AJAX the "wordpress way" or use the newer REST stuff.
Directly answering your question of "how to use Wordpress functions in external PHP" see https://wordpress.stackexchange.com/questions/47049/what-is-the-correct-way-to-use-wordpress-functions-outside-wordpress-files
<?php
define('WP_USE_THEMES', false); // although you might want that to be true if you're using theme data?
require('./wp-load.php');
?>
Or maybe consider doing all that stuff in a shortcode that takes the place key and/or api key, e.g. [get-place-info placekey="xxxxx" apiKey="xxxx"] so your user can dump it anywhere, unless you specifically need it available to javascript (heck, you could have the shortcode dump JSON into the page).
Everything I look at online is showing how to use OAuth & Curl to make a POST request, but I want to make a get request to the Mailchimp API and I'm not getting any response it seems. I've already managed to go through the authentication and get the user's token & api URL. Now I'm just trying to pull in their lists. Here's the CURL code I've got currently:
$headers = array(
"Content-type: application/json",
"Authorization: OAuth ".$user['mct']
);
$curl = curl_init();
curl_setopt_array($curl,array(
CURLOPT_URL => "https://".$user['dc'].".api.mailchimp.com/3.0/lists",
CURLOPT_USERAGENT => "oauth2-draft-v10",
CURLOPT_HTTPHEADER => $headers,
CURLOPT_ENCODING => ''
));
$tresp = curl_exec($curl);
$lists = json_decode($tresp,true);
curl_close($curl);
Assuming $user['mct'] and $user['dc'] contain the proper values, any idea what I'm doing wrong here?
In case anyone ends up googling and finding this, my problem was that the user information I was getting from wordpress' get_results() function was an object and not an array. Took me forever to realize because for some reason this part of my plugin is preventing me from using print_r().
Now that it's actually going to the Mailchimp API I'm able to get and debug whatever error they're sending back.
I'm having some issues with calling WordPress XML-RPC via cURL in PHP. It's a WordPress.com hosted blog, and the XML-RPC file is located at http://sunseekerblogbook.com/xmlrpc.php.
Starting yesterday (or at least, yesterday was when it was noticed), cURL has been failing with error #52: Empty reply from server.
The code snippet we're using is below:
$ch = curl_init('http://sunseekerblogbook.com/xmlrpc.php');
curl_setopt_array($ch, [
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => [
'Content-Type: text/xml'
],
CURLOPT_POSTFIELDS => xmlrpc_encode_request('wp.getPosts', [
1,
WP_USERNAME,
WP_PASSWORD,
[
'number' => 15
]
]),
CURLOPT_RETURNTRANSFER => true
]);
$ret = curl_exec($ch);
$data = xmlrpc_decode($ret, 'UTF-8');
Using cURL directly however, everything returns exactly as expected:
$output = [];
exec('curl -d "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall><methodName>wp.getPosts</methodName><params><param><value><int>1</int></value></param><param><value><string>' . WP_USERNAME . '</string></value></param><param><value><string>' . WP_PASSWORD . '</string></value></param><param><value><struct><member><name>number</name><value><int>15</int></value></member></struct></value></param></params></methodCall>" sunseekerblogbook.com/xmlrpc.php', $output);
$data = xmlrpc_decode(implode('', $output), 'UTF-8');
We've been successfully able to query WordPress since July 2013, and we're at a dead-end as to why this has happened. It doesn't look like PHP or cURL have been updated/changed recently on the server, but the first code snippet has failed on every server we've tried it on now (with PHP 5.4+).
Using the http://sunseekerblogbook.wordpress.com/xmlrpc.php link gives the same issue.
Is there anything missing from the PHP code that would cause this issue? That it's suddenly stopped working over 12 months down the line is what has flummoxed me.
Managed to fix it. Looking at the headers sent by cURL, the only differences were that the cURL command line uses Content-Type: application/x-www-form-urlencoded and that the user agent was set to User-Agent: curl/7.30.0.
The choice of content type didn't affect it, but setting a user agent sorted it! It seems WordPress.com (but not self-hosted WordPress.org sites running the latest v3.9.2) now requires a user agent for XML-RPC requests, though this hasn't been documented anywhere that I can find.
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.