Google chatbot has a webhook where users can automate chat there is a small python script to make it work but I want to do the same using php. I am using curl to hit the post method
$apiUrl = 'https://chat.googleapis.com/v1/spaces/AAAAbvoeZ0w/messages?key=AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI&token=h2OIcCLB6_f_XxhPX6lugnGizE88ZBBpJURFHtPfh_0%253D';
$ch = curl_init($apiUrl);
//$kid_userid = "IXM_".session('user_data')['userID'];
$data = '{"text": "All Mock Test are Free "}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
after hitting this code I am getting errors.
{
error: {
code: 400,
message: "Invalid request token h2OIcCLB6_f_XxhPX6lugnGizE88ZBBpJURFHtPfh_0%3D",
status: "INVALID_ARGUMENT"
}
}
but same code when I am running on python it is working perfectly. Any Help will be very helpful. Thanks for the help in advance and I tried my best to explain .Sorry If I have made any mistake.
This code works for me. I have it from another SO answer, maybe the issue is the missing CURLOPT_POST...
$uri = "https://chat.googleapis.com/v1/spaces/AAAAgcWhzwk/messages?key=MY_KEYI&token=MY_TOKEN";
$msg = '*Testing Curl PHP message to Google Chat*\n\n Description';
$params = '{"text": "'.$msg.'"}';
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
The above code which I have shared is correct actually I just added the new bot URI and it worked. THanks, everyone for your help.
Related
This really has me perplexed and other examples on Stackoverflow have not helped.
If I type into the browser :
https://api.bitfinex.com/v2/book/tXMRUSD/P0
I will get an long array of data which is correct.
With the following simple code :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.bitfinex.com/v2/book/tXMRUSD/P0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
I get :
bool(false)
The only difference is my browser fetches the data directly, while the PHP code is on a local website hosted using IIS on windows 10.
I've tried everything and just can't see where I'm going wrong. Any help will be most appreciated. (Ironically, all the authenticated and encrypted POST code works fine and I'm stuck on the easy stuff!)
Try This code it will work!!!!
<?php
$url= "https://api.bitfinex.com/v2/book/tXMRUSD/P0";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
?>
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 am trying to access the Toggl Reporting API.
I tried following in PHP with cURL, which connects to the API but gives the following error message: 'This method may not be used.' Any light on why this is the case would be useful as I'm very new to webservices. I may be missing something obvious or totally going the wrong way about it, so apologies if this is the case.
<?php
$userAgent = 'xxx';//username
$token = 'xxx';//token
$returned_content = get_data('https://toggl.com/reports/api/v2/summary?&workspace_id=[workspaceid]&since=2013-05-19&until=2013-05-20&user_agent=[username here]');
print_r($returned_content);
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERPWD, $token.':api_token');
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
Edit: I tried a different approach. If I run the following code, I no longer receive any error messages, so the code seems to be executing but I can't print the response to the screen. Is there something specific I need to do to view the output other than print_r?(Toggl API returns JSON). Thanks.
$json = curl%20-v%20-u%[myapitoken]:api_token%20https://toggl.com/reports/api/v2/weekly?workspace_id=[id]&wsid=282507&since=2012-08-19&until=2013-09-20&user_agent=[user].json;
print_r($json);
Edit: Finally resolved! Code is as follows:
$workspace_id = '[id here]';
$user_agent = '[user agent here]'; // no spaces
$api_token = '[token here]';
$report_url = 'https://toggl.com/reports/api/v2/weekly?user_agent='.$user_agent.'&since=2013-08-01&until=2013-09-01';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_USERPWD, $api_token . ':api_token');
curl_setopt($ch, CURLOPT_URL, $report_url . '&workspace_id=' . $workspace_id);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
$result = json_encode($result);
Hope this helps someone in the future!
As I understand, you are receiving this message because of CURLOPT_SSL_VERIFYPEER == FALSE.
Try to remove this string from the code:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
Maybe I wrong, but I think with this option you are receiving "HTTP 501 Not Implemented" error from the Toggl server, which contains exactly the same message, "This method may not be used."
Here is my code:
$params = array();
$params['shared_link'] = array("access"=> "Open");
$params = json_encode($params);
echo $params;
$key = "[key]";
$token = "[token]";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.box.com/2.0/folders/[folder_id]/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
"Authorization: BoxAuth api_key=$key&auth_token=$token",'Content-Length: ' . strlen($params), 'X-HTTP-Method-Override: PUT'));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
I am not being able to create a shared link. I get this response from box.net:
{"type":"error","status":500,"code":"internal_server_error","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Internal Server Error","request_id":"79086734650bfaf56c7894"}
Can somebody, please, help me on this?
Thanks!
Marcelo
Looking at the URL returned in their response, they give this information for 500 errors:
5xx
The request is fine, but something is wrong on Box’s end
So it sounds like you would need to contact Box about the issue.
Luckily, i could solve my problem.
The problem was that i wanted to create an "open" shared link with an enterprise token and apparently it is not possible (i am not 100% sure but, as per my attempts, i think so).
Thanks everyone for the help.
Marcelo
I know nothing about implementing an API. I do know PHP a bit. I have a situation where I need to call a REST API method to purge cache on a CDN server. Can somebody help me with some sample code?
The following is the sample request:
PUT <<url>>
Authorization: TOK:12345-12345
Accept: application/json
Content-Type: application/json
Host: api.edgecast.com
Content-Length: 87
{
"MediaPath":"<<urlhere>>"
"MediaType":"3"
}
Can somebody help me with code to implement this rest api request?
Thanks in advance.
I had to find the hard way too. This has been tested (with slight modifications from my original code)
//## GoGrid PHP REST API Call
define('TOKEN','XXXXX-XXXXX-XXXXX-XXXXXX'); // found on the cdn admin My Settings
define('ACCOUNT_NUMBER','XXXX'); // found on the cdn admin Top Right corner
function purgeCacheFileFromCDN($urlToPurge) {
//## Build the request
$request_params = (object) array('MediaPath' => $urlToPurge, 'MediaType' => 8); // MediaType 8=small 3=large
$data = json_encode($request_params);
//## setup the connection and call.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.edgecast.com/v2/mcc/customers/'.ACCOUNT_NUMBER.'/edge/purge');
curl_setopt($ch, CURLOPT_PORT , 443);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1); // For debugging
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1); // no caching
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); // no caching
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: tok:'.TOKEN, 'Content-Type: application/json','Accept: application/json', 'Content-length: '.strlen($data)));
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch);
curl_close($ch);
//## check if error
if ($httpCode['http_code'] != 200) {
echo 'Error reported: '.print_r(array($head,$httpCode),1); // output it to stdout this will be emailed to me via cron capture.
}
}
Was too lazy to write from the scratch so copied from amazingly pink site that Google advises in the first page of results.
$data = array("a" => $a);
$ch = curl_init($this->_serviceUrl . $id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
if(!$response) {
return false;
}
PS: The source search request: http://www.google.ru/search?q=php+sample+put+request+curl
Here is my source gist for my fully implemented Grunt task for anyone else thinking about working with the EdgeCast API. You'll find in my example that I use a node module to execute the curl command which purges the CDN.
This was that I ended up with after spending hours trying to get an HTTP request to work within Node. I was able to get one working in Ruby and Python, but did not meet the requirements of this project.