I am trying to use the path
POST /d2l/api/lp/(D2LVERSION: version)/users/
and am running into an error. I'm hoping there is something not in the documentation that I am missing, because from what I can tell, this should work.
HTTP/1.1 400 Bad request
Cache-Control: private
Content-Length: 0
Server: Microsoft-IIS/7.5
X-XSS-Protection: 0
X-Powered-By: ASP.NET
Date: Wed, 23 Jan 2013 18:37:07 GMT
The data I'm sending looks like this:
{
"OrgDefinedId":"190006002",
"FirstName":"Jesty",
"MiddleName":"Q",
"LastName":"McTest",
"ExternalEmail":"privateemail#gmail.com",
"UserName":"190006002",
"RoleId":115,
"IsActive":true,
"SendCreationEmail":true
}
My PHP code looks like this at the moment:
$random_hash = "xxBOUNDARYxx";
$request ="--".$random_hash.
"\r\nContent-Type: application/json\r\n\r\n$data\r\n\r\n--".
$random_hash;
$length=strlen($request);
$url = $opContext->createAuthenticatedUri($url,"POST");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("HTTP/1.1", "Content-Type: multipart/form-data; boundary=xxBOUNDARYxx","Content-Length:".$length));
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
return curl_exec($ch);
Ok, after several hours of tinkering I figured it out. The original code I posted was based off of code that worked for posting the profile image through the API. The code for just posting JSON was much simpler. Here is a generic PHP function for sending JSON to Valence through the POST method
/*
* $opContext is your user context object
* $url is the route
* $data is the well formed JSON in a string ( use json_encode() )
* $ct is the count of json fields
*/
static function basic_post($opContext,$url,$data,$ct){
$url = $opContext->createAuthenticatedUri($url,"POST");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_POST,$ct);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
return curl_exec($ch);
}
Related
I was able to get an authorisation code but I have problems with getting Access Token.
While making a Request to get an Access Token, Every time I get Fatal error: Uncaught Exception: HTTP 404 {"error": "Resource not found"}
Here is my request:
$url = "https://api.etsy.com/v3/public/oauth/token?grant_type=authorization_code&client_id={MY_ Etsy_App_ API_ Key }&redirect_uri={MY_REDIRECT_URL}&code=".$code."&code_verifier=".$code_verifier;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array(
'x-api-key: ={ MY_ Etsy_App_ API_ Key } ',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response_body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (intval($status) != 200) throw new Exception("HTTP $status\n$response_body");
Where can be a mistake?
You're making a GET request to the token endpoint, but you should be making a POST request and sending the parameters in the request body.
curl_setopt($curl, CURLOPT_POST, true);
$headers = array(
"Content-Type: application/x-www-form-urlencoded",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = "grant_type=authorization_code&code=".$code."&redirect_uri="...
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
I think you don't need the x-api-key header when making a call to the token endpoint.
Sorry to repost this question as it was asked a few times, but no answer was ok for me.
My app don't wake up anymore on background notification.
So I updated my php script and added HEADERs field as documented here and here
But no luck.
Here my sample code :
<?php
$device_token = 'd98e1d488acac56305a9f83b...b616bc5b5b3bf238dc';
//$device_token = '27b296397f81f48590a....fe2742e0b2a051ae2cefffded85d7';
// Put your private key's passphrase here:
$pem_secret = '';
$pem_file = 'pushcert.pem';
$url = "https://api.development.push.apple.com/3/device/$device_token";
//$body = '{"aps":{"alert":"balbla","badge":0,"sound":"default"}}';
$body = '{"aps":{"content-available":1},"acme1":"bar"}';
$header = ['apns-topic'=>'com.blabla.bla',
'apns-push-type'=>'background',
'apns-priority'=>'5'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_SSLCERT, $pem_file);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pem_secret);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$information = curl_getinfo($ch);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//On successful response you should get true in the response and a status code of 200
//A list of responses and status codes is available at
//https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1
//var_dump($header);
//var_dump($information);
var_dump($response);
var_dump($httpcode);
?>
I never enter the iOS methode to catch this notification :
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[[NSUserDefaults standardUserDefaults] setValue:userInfo forKey:#"testtesttest"];
// Method called when user click on notification and application is in background
application.applicationIconBadgeNumber = 0;
NSLog(#"did received remote notification"); }
Also in my device console, it seems that the device is canceling my notifications or something a don't get.
Also not that standard notifications (not in Background) works fine.
testing iOS 13.3.1
Thanks a lot for any help you could get me.
Background mode enabled :
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>location</string>
<string>processing</string>
<string>remote-notification</string>
</array>
App Settings also configured :
I managed to make it Work with that sample code:
<?php
// open connection
if (!defined('CURL_HTTP_VERSION_2_0')) {
define('CURL_HTTP_VERSION_2_0', 3);
}
$device_token = 'd56e9ef8b5ea558f07584cf8...498824ece60a36229446a2003116'; //David
// Put your private key's passphrase here:
$pem_secret = '';
$pem_file = 'pushcert.pem';
$url = "https://api.development.push.apple.com/3/device/$device_token";
$app_bundle_id = "com.blabla.blabla";
//$body = '{"aps":{"alert":"balbla","badge":0,"sound":"default"}}';
$body = '{
"aps":{"content-available":1},
"type":"LOCATION",
"idRequest":"538EAC3E765A4BDC89B554C40F5B14EF"
}';
$headers = array(
"apns-topic: {$app_bundle_id}",
"User-Agent: My Sender",
"apns-push-type:background",
"apns-priority:5"
);
var_dump($body);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PORT,443);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST,TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSLCERT, $pem_file);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
$response = curl_exec($ch);
$information = curl_getinfo($ch,CURLOPT_HTTPHEADER);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//On successful response you should get true in the response and a status code of 200
//A list of responses and status codes is available at
//https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1
//var_dump($header);
var_dump($information);
var_dump($response);
var_dump($httpcode);
?>
Well at least it seems that the application is waking up (seen in the device log)
So this morning I launch the code from yesterday (with no modifications at all) and it worked for a few times, like 4 or 5 times.
Now it doesn't work anymore. Same as yesterday (iOS system seems to catch the payload, but application is not waked up)
-> conclusion, this a system limitation, and this is so confusing when you need to perfom tests. I'm gonna put this in production with no assurance it will work properly...
Here is the situation.
I have an Android app, which can send voice file with HTTP POST anywhere I specify. Also I would like to download these voice files to Telegram bot automatically. I wrote a PHP bot and I can accept and translate to Telegram any fields that come in said POST. One thing I miss is the file itself.
I don't know how to "say" to telegram API I download a file.
So what do I have: present file in $_FILE stream (I can read the name of it), I throw it in sendVoice function as $voice, also I do know I have to use simple multipart/form-data POST method to send file.
My question is: is it possible to use something but CURL to POST right from PHP? If not, what am I missing then? Other methods I use are messages and photo sending via file_get_contents(), I wonder if it's still suitable for voice file?
Here is the code I have now, which I found somewhere (I have 0 cURL knowledge):
function sendVoice($chat_id, $voice, $caption) {
$boundary = uniqid();
$delimiter = '-------------' . $boundary;
$fields = array(
"chat_id" => $chat_id,
"caption" => $caption
);
$post_data = build_data_files($boundary, $fields, $voice);
$ch = curl_init($GLOBALS['api'].'/sendVoice');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
//"Authorization: Bearer $TOKEN",
"Content-Type: multipart/form-data; boundary=" . $delimiter,
"Content-Length: " . strlen($post_data))
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_exec($ch);
curl_close($ch);
}
function build_data_files($boundary, $fields, $files){
$data = '';
$eol = "\r\n";
$delimiter = '-------------' . $boundary;
foreach ($fields as $name => $content) {
$data .= "--" . $delimiter . $eol
. 'Content-Disposition: form-data; name="' . $name . "\"".$eol.$eol
. $content . $eol;
}
foreach ($files as $name => $content) {
$data .= "--" . $delimiter . $eol
. 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . '"' . $eol
//. 'Content-Type: image/png'.$eol
. 'Content-Transfer-Encoding: binary'.$eol
;
$data .= $eol;
$data .= $content . $eol;
}
$data .= "--" . $delimiter . "--".$eol;
return $data;
}
This was actally much easier. Since we have file present in $_FILES, we can do all of the cURL in just sendVoice() function without any hassle of rebinding boundaries or specifying content types.
Here is the final code which worked for me:
function sendVoice($chat_id, $voice, $caption) {
$filepath = realpath($_FILES['file']['tmp_name']);
$post_data = array(
'chat_id' => $chat_id,
'voice' => new CURLFile($filepath),
'caption' => $caption
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $GLOBALS['api'].'/sendVoice');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_exec($ch);
curl_close($ch);
}
That will download an .OGG file as voice message (because I do not want any files to appear in my music player). Later it is possible to just add more fields such as duration in $post_data array and Telegram would eat them accordingly.
I was using the neo4j query as below till now but I am wondering if it's making neo4j out of memory as I am not committing anywhere(I guess its auto-commit) .But the query works fine only worried if neo4j will shutdown or slow down due to my query. I really appreciate any help.
sample.php
<?php
if (!empty($array)) {
if( get_magic_quotes_gpc() ) {
$array = stripslashes( $array );
}
$newstr = json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $array), true );
if(!empty($newstr)){
$j=0;
foreach($newstr as $item) { //foreach element in $arr
$category_id = $item['category_id']; //etc
$category_name = $item['category_name'];
$category = $item['category'];
$data2 .=' MERGE (u'.$j.':Category {name:"'.$category_name.'",id:"'.$category_id.'",category:"'.$category
.'"}) ';
$j=$j+1;
}
$data2 = array("query" =>$data2);
$data_string = json_encode($data2);
$ch = curl_init('http://localhost:7474/db/data/cypher');
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);
}else{
echo "null";
}
}
?>
You are using the Legacy Cypher HTTP Endpoint, which automatically commits at the end of a successful query. So, there is no need to worry about losing your data.
However, the legacy endpoint is now deprecated, so you should consider changing over to the Transactional endpoint.
Also, as a suggestion, you can change this snippet:
' MERGE (u'.$j.':Category {name:"'
to:
' MERGE (:Category {name:"'
Your query never uses the uxxx identifiers, so there is no need to define them in the first place.
$param = 'get.php';
$url = 'http://mysite.org/'. $param;
$__post = 'option=1&option2=225';
mysite.org/get.php - exists for 100%, when I open them in browser everything works.
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $__post);
$result = curl_exec($c);
curl_close($c);
print_r($result); // show 404 o_O
I test it using sniffer and CURL pass URL :
http://mysite.org
not
http://mysite.org/get.php
when I do somethink like that :
curl_setopt($c, CURLOPT_URL,'http://mysite.org/get.php');
everything works fine... so... please help me :D What I do wrong, certainly it is a small error but I don't see where he is :/