I'm trying to add a new user to my Vanilla forum and unfortunately I don't understand what's wrong with my code. GET request to read user data works fine butPOST requests result in a "bool(false)".
As I understand it, that's already a response from the API, which obviously can't do anything with my request, right?
But what's wrong with that? Do I have to somehow send the JSON data as a 'body'? Anyone have an idea? Thank you!!!
This is what my code currently looks like:
<?php
// post with absolute url = Invalid CSRF token error
// $url = "https://example.com/app/vanilla/api/v2/users";
$url = "/app/vanilla/api/v2/users";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
$headers = array(
"Accept: application/json",
"Authorization: Bearer {va.jOh189hry7463klur_kcw8UomJU36782.HGqvdQ.RQon456}"
);
$data = array(
'bypassSpam' => 'false',
'email' => 'info#mysite.de',
'emailConfirmed' => 'true',
'name' => 'Theo Tester',
'password' => '1234567890',
'photo' => '',
'roleID' => [33]
);
$payload = json_encode(array($data));
// Attach encoded JSON string to the POST fields
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
// Set the content type to application/json
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
// Return response instead of outputting
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
?>
This is what the vanilla API documentation looks like (please see picture below or link):
(https://success.vanillaforums.com/kb/articles/202-api-v2-reference-endpoints#/Users/post_users)
And this just for completeness is the code for GET requests as it works for me:
<?php
$url = "https://example.com/app/vanilla/api/v2/roles";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Accept: application/json",
"Authorization: Bearer {va.jOh189hry7463klur_kcw8UomJU36782.HGqvdQ.RQon456}",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
?>
Related
I want to upload the media via Whatsapp cloud API in order to send the media to WhatsApp, I have followed the link below, but having the stated issue.
https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#get-media-id
Array ( [error] => Array ( [message] => (#100) The parameter messaging_product is required. [type] => OAuthException [code] => 100 [fbtrace_id] => AQPftb9Bf343375gQ_QytxNen ) ) 400
My Code is
// #1640191015925.jpg on my root folder
$post = [
'file' => '#1640191015925.jpg;type=image/jpeg',
'messaging_product' => 'whatsapp'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v13.0/Phone-Number-ID/media');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$headers = array();
$YOUR_WHATSAPP_ACCESS_TOKEN = '+++++++';
$headers[] = "Authorization: Bearer $YOUR_WHATSAPP_ACCESS_TOKEN";
$headers[] = "Content-Type: image/jpeg";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = json_decode(curl_exec($ch), true);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//$MEDIA_OBJECT_ID = $result['media'][0]['id']; //MEDIA OBJECT ID
print_r($result);
print_r($httpcode);
?> ```
I had the same issue, after some digging around I wrote this function to upload a file and return the id.
function waFileUploader($token, $file, $senderid){
$mime = mime_content_type($file);
$info = pathinfo($file);
$name = $info['basename'];
$curlfile = new CURLFile($file, $mime, $name);
$filedata = array("messaging_product"=>"whatsapp", "file" => $curlfile);
$url = 'https://graph.facebook.com/v13.0/' . $senderid . '/media';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Authorization: Bearer $token", "Content-Type: multipart/form-data"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $filedata);
$r = curl_exec($ch);
curl_close($ch);
$id = json_decode($r, true);
return $id["id"];
}
I am new to cURL from PHP. I am trying to send file URL as post field and API Key as header to the background remover API to retrieve the background removed image. The error says:
{"errors":[{"title":"No image given","code":"missing_source","detail":"Please provide the source image in the image_url, image_file or image_file_b64 parameter."}]}
My code:
<?php
$url='https://api.remove.bg/v1.0/removebg';
$ch = curl_init($url);
$data = array('image_url'=> 'https://www.requestingservicebyme.com/upload/imageexample.jpg');
$headers1=['X-API-Key:xxxxxxxxxxxxxxx',
'Content-Type:application/json'];
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$buffer = curl_exec($ch);
if (empty($buffer)) {
echo " buffer is empty ";
} else{
echo $buffer;
}
curl_close($ch);
?>
I was also working on it using the curl I got success in that ..!
Change your headers
Remove the application type json from headers as this api returns the data in Image content format.
$x = curl_init();
$headers=['X-API-Key:xxxxxxxxxxxxxxxxx'];
$data = array (
'image_url' => "https://amblin.com/wp-content/uploads/2019/09/castaway_2000_photo_29.jpg",
'size' => "auto",
);
$url = 'https://api.remove.bg/v1.0/removebg';
// $post = http_build_query($data);
$x = curl_init($url);
curl_setopt($x, CURLOPT_POST, true);
curl_setopt($x, CURLOPT_RETURNTRANSFER, true);
curl_setopt($x, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($x, CURLOPT_HTTPHEADER, $headers);
curl_setopt($x, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($x, CURLOPT_POSTFIELDS, $data);
$y = curl_exec($x);
curl_close($x);
//And then save it using this code
$fp = fopen("result.jpg", "wb");
fwrite($fp, $y);
fclose($fp);
I am having some issue running the api to send email with attachment via Mail Gun.I tried to run the api via postman and it runs perfectly fine so i know there is no problem with the api but i am unable to send a request via code.
I have added a code sample below:
<?php
$curl_post_data=array(
'from' => 'Excited User <noreply#test.com>',
'to' => 'test#gmail.com',
'subject' => 'Hello',
'text' => 'test'
//,'attachment[1]' => '#https://www.smsglobal.com/docs/HTTP-2WAY.pdf'
,array( 'attachment' => 'https://www.smsglobal.com/docs/HTTP-2WAY.pdf')
);
$service_url = 'https://api.mailgun.net/v3/test.com/messages';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "api:key-testtesttesttes");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);
var_dump($response);
?>
I was able to solve the issue.
My Findings:
1-) You cannot give a live url address as attachment http://test.com/images/logo.png will not work.
2-) only address of files hosted on the server can be given
/var/images/logo.png
<?php
$filePath='#0Wealth_AC_AMF.pdf';
$curl_post_data=array(
'from' => 'Excited User <noreply#test.com>',
'to' => 'test#gmail.com',
'subject' => 'Hello',
'text' => 'test',
'attachment[1]' => $filePath
);
$service_url = 'https://api.mailgun.net/v3/test.com/messages';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "api:key-test");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);
var_dump($response);
?>
I am trying to create a phonegap app by using their createapp build API as described in
http://docs.build.phonegap.com/en_US/developer_api_write.md.html
<?php
$data = array(
"file" => "#http://mywebsite.com/tmp/www.tar.gz",
//"file" => "#actual path to/www.tar.gz",
"data" => '{"title":"MyTestApp","create_method":"file"}'
);
$myusername = 'my build.phonegap user id';
$password = 'my build.phonegap password';
$handle = curl_init();
curl_setopt($handle, CURLOPT_USERPWD, $myusername.':'.$password);
curl_setopt($handle, CURLOPT_URL, "https://build.phonegap.com/api/v1/apps");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_HTTPHEADER, array(
'Content-type: multipart/form-data;',
));
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($handle);
print_r($response);
?>
The www.tar.gz file is a valid file and it can be even download from the URL.
The code shows the below error
{"error":"Unable to create app: input is not a file"}
I raised this with phonegap support website and It looks my question doesn't get anyone's attention there. :-(
https://c.getsatisfaction.com/nitobi/topics/unable-to-create-app-input-is-not-a-file-df668pmjm93np
Thanks #mradionov at https://github.com/mradionov/phonegap-build-api/issues/3
The problem is just because of the cURL changed in php and nothing to do with the phonegap api releases.
It looks the file upload method via curl got changed in php 5.6 version.
The below solved the issue
<?php
$data = array(
"file" => new CurlFile("path to the file/abcd.tar.gz"),
"data" => '{"title":"MyTestApp","create_method":"file"}'
);
$myusername = 'phonegap user id';
$password = 'phonegap password';
$handle = curl_init();
curl_setopt($handle, CURLOPT_USERPWD, $myusername.':'.$password);
curl_setopt($handle, CURLOPT_URL, "https://build.phonegap.com/api/v1/apps");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_HTTPHEADER, array(
'Content-type: multipart/form-data;',
));
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($handle);
print_r($response);
?>
In case if we use the OAuth then use the below
<?php
$data = array(
"file" => new CurlFile("path to file/abcd.tar.gz"),
"data" => '{"title":"MyTestApp","create_method":"file"}'
);
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, "https://build.phonegap.com/api/v1/apps?access_token=accesstoken");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_HTTPHEADER, array(
'Content-type: multipart/form-data;',
));
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($handle);
print_r($response);
?>
#Malaiselvan,
Phonegap 2.x is obsolete. It is no longer supported. None of those examples work any longer.
As of 23 Sep 2015, the blog post is entitled PhoneGap 5.2.0 Now Available on Build.
The means we are on Phonegap version 5.x.
If you want build your own remote IDE, then you want to use this documentation
http://docs.build.phonegap.com/en_US/developer_api_api.md.html#PhoneGap%20Build%20Developer%20API
Best of Luck
$ch=curl_init();
$postfield = "action=login&lgname=d&lgpassword=Password&format=json";
$url = "http://wiki.signa.com/api.php"; //url to wiki's api
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfield);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = json_decode(curl_exec($ch));
curl_close($ch);
print_r($output);
$token = $output->login->token;
$session = $output->login->sessionid;
$ch=curl_init();
$postfield = "action=login&lgname=d&lgpassword=Password&lgtoken={$token}";
$url = "http://wiki.signa.com/api.php"; //url to wiki's api
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfield);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
print_r($output);
die;
With the first request I get this object:
stdClass Object
(
[login] => stdClass Object
(
[result] => NeedToken
[token] => ad61fadb829e5cd44b0062463b7cc2d2
[cookieprefix] => wikisign_mediawiki
[sessionid] => ebb892881eed27554161234916d00480
)
)
I'm using the token to do a second request, but I get result = NeedToken. It should be success since I'm sending the token now.
I noticed the documentation says:
Send a login request with POST, with confirmation token in body and the session cookie (e.g. enwiki_session) in header as returned from previous request.
I'm not totally clear on that. I'm assuming I'm having issues because I'm not sending the session cookie in the header. Do I need to set a cookie prior to the second request?
Yes, you need to process all Set-Cookie Headers to login succesfully.
Documentation is very specific on this:
This request will also return a session cookie in the HTTP header
(Set-Cookie: enwikiSession=17ab96bd8ffbe8ca58a78657a918558e; path=/;
domain=.wikipedia.org; HttpOnly) that you have to return for the
second request if your framework does not do this automatically
A successful action=login request will set cookies needed to be
considered logged in. Many frameworks will handle these cookies
automatically (such as the cookiejar in cURL). If so, by all means
take advantage of this. If not, the most reliable method is to parse
them from the HTTP response's Set-Cookie headers.
It's an old subject, but I'll answer for those who are interested :)
In fact you need to keep your cookies in a file and use them again in your second attempt to login.
Something like that :
$path_cookie = 'connexion_temporaire.txt';
if (!file_exists($path_cookie)) touch($path_cookie);
$postfields = array(
'action' => 'login',
'format'=> 'json',
'lgname' => $login,
'lgpassword' => $pass
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $lienTestWiki);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_COOKIEJAR, $path_cookie); // you put your cookie in the file
$connexion=curl_exec($curl);
$json_connexion = json_decode($connexion, true);
$tokenConnexion=$json_connexion['login']['token']; // you take the token and keep it in a var for your second login
// /!\ don't close the curl conection or initialize a new one or your session id will change !
$postfields = array(
'action' => 'login',
'format'=> 'json',
'lgtoken' => $tokenConnexion,
'lgname' => $login,
'lgpassword' => $pass
);
curl_setopt($curl, CURLOPT_URL, $lienTestWiki);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_COOKIEFILE, $path_cookie); //get the previous cookie
$connexionToken=curl_exec($curl);
var_dump($connexionToken);
When running this you should see a success this time :)