I'm trying to send POST with JSON to an external Flask server from my MyBB Forum with CURL. The code works outside of MyBB (I tested it on REPL), but I couldn't make it work in MyBB. CURL is enabled on the server and I can send PHP to PHP without any problem. Thanks in advance.
function send($class, $method) {
$dict = $this->dict; // an array
$dict["sender"] = "panel";
$json = json_encode($dict);
$ch = curl_init("http://blabla/operation/" . $class . "/" . $method);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json))
);
$result = curl_exec($ch);
return $result;
}
Here is the full code: https://hastebin.com/mecubapenu.php
Edit:
As a note, I can't see the request on Flask. It seems like it's not sending it.
Related
Trying to create a php script which will receive JSON from Dialogflow webhook.
The data then needs to be parsed and
<?php
$input = json_decode(file_get_contents('php://input'), true);
$messageId = $input["resolvedQuery"];
$text = print_r($input,true);
file_put_contents('output.txt', var_export($text, TRUE));
$url = "https://autoremotejoaomgcd.appspot.com/sendmessage?key=APAue83jLrt7xFeQoGjgKq&message=" . $messageId;
$data = array("result" => "resolvedQuery");
$ch = curl_init($url);
$data_string = json_encode($messageId);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, array("resolvedQuery"=>$data_string));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Content-Type:application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
curl_close($ch);
?>
The data i need is from resolvedQuery but im not getting any output.
Would someone point me in the right direction please
So from my understanding, you want resolvedQuery from api.ai in your webhook. All you need to do is change:
$messageId = $input["resolvedQuery"];
to
$messageId = $input['result']['resolvedQuery'];
Check and tell me if it's working. I did skype, fb, and google integrations in php so if you need any help feel free to ask.
How can I call transltr api from my localhost using php? I have zero knowledge in calling api and what to type in the php file. Thank you in advance !
php cUrl library must be loaded and enabled.
You can check this using phpinfo file.
To use API, you can try code below.
<?php
$params = array('text'=>'something', 'from'=>'en', 'to'=>'de');
$data_string = json_encode($params);
$ch = curl_init('http://www.transltr.org/api/translate');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
if(!$result){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
echo $result;
?>
Hope this helps
You can use curl_exec function from CURL extension.
In the most simple form it looks like:
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$result = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
Now $result holds what you need.
I've been using the cloud environment, which works fine. I just recently downloaded the standalone version and am successfully running it on my ubuntu server. All the PHP SDK calls work, and the api/v1/data/[X Table Name] CuRL requests work.
However, I cannot get the CuRL request for valid login and logout to work. With the cloud implementation this was working:
function isValidToken($userToken){
$ch = curl_init();
$appId = APP_ID_FOR_CLOUD;
$restKey = REST_KEY_FOR_CLOUD;
$headers = array("application-id:$appId","secret-key:$restKey","application-type:REST");
curl_setopt($ch, CURLOPT_URL, "https://api.backendless.com/v1/users/isvalidusertoken/" . $userToken);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
This returns the proper response.
Now the only thing that changes are the IDs, keys, and URL, but it cannot find the requested URL. Here is the call to the standalone implementation:
function isValidToken($userToken){
$ch = curl_init();
$appId = APP_ID_STANDALONE;
$restKey = REST_KEY_STANDALONE;
$headers = array("application-id:$appId","secret-key:$restKey","application-type:REST");
curl_setopt($ch, CURLOPT_URL, "http://[my_server_ip_address]/v1/users/isvalidusertoken/" . $userToken);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
return $response;
}
The response I get is:
The requested URL /v1/users/isvalidusertoken/35A977A7-60DB-3772-FFC9-29E72AFAA200 was not found on this server.
Does anyone know how to resolve this issue? Thanks in advance!
I was just able to figure this out, the issue was due to a simple typo in the URL. For anyone who may make the same mistake, the standalone URL (for isvaliduesrtoken) is:
http://[my_server_ip_address]/api/<your_app_version>/users/isvalidusertoken/<user-token>
I'm implementing GCM on my website from this tutorial
All work fine when I send a Curl query by command line on the dev windows, I see one call of the event listener.
But, when I send query by PHP, there are two call of the listener. I don't know where that from....
My PHP code :
<?php
//request url
$url = 'https://android.googleapis.com/gcm/send';
//your api key
$apiKey = $apiKey;
//registration ids
$registrationIDs = array('fwt_qmI-Rlo:A...eencfYg2_0WU');
$fields = array('registration_ids' => $registrationIDs);
//http header
$headers = array('Authorization: key=' . $apiKey,
'Content-Type: application/json');
//curl connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
And my JS code :
self.addEventListener('push', function(event) {
console.log('Push message', event);
});
I never realy used Curl on PHP, and maybe there are errors...
Can someone help me ?
Thanks.
I've a basic (free) account at box.com and I've build a form in PHP where user can select a file and pass that file to upload.php. Now, I need to connect to box.com servers and use my account to store that file. I have my own login used here.
How do I implement this?
Currently I'm doing this:
<pre><?php
$parent = array();
$parent['id'] = '0';
$params = array();
$params['name'] = 'Testfolder';
$params['parent'] = $parent;
$params = json_encode($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.box.com/2.0/folders");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("'Content-Type: application/json',
'Content-Length: ' . strlen($params), Authorization: BoxAuth api_key=[api]&auth_token=[token]"));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
?>
</pre>
But currently [token] is very very temporary and it isn't working as well. Please help!
Take a look at this. I have had success using it and found it to be the best framework out there for box-api and php https://github.com/golchha21/BoxPHPAPI