file_put_contents failed to open stream: no such file or directory - php-5.6

I create a php file that need to create 2 another files via file_put_contents. The problem when I run this code, the lastinsert.txt and newinsert.txt can't be created. I changed the path of these files but I get the same problems. I tried to create the file manually under my web repository it work fine.
this is my code:
function sendPushNotification($registration_ids, $message) {
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registration_ids,
'data' => $message,
);
define('GOOGLE_API_KEY', 'AIzaSyC36gV0pWJgP61mS0JYHadTB66Lz92_1RU');
$headers = array(
'Authorization:key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
echo json_encode($fields);
$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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if($result === false)
die('Curl failed ' . curl_error());
curl_close($ch);
return $result;
}
echo $crc1=(int)file_get_contents('http://mywebsite/public_html/CodeIgniter/lastinsert.txt');
$pushStatus = '';
$querycount="SELECT id_relation FROM relations ORDER BY id_relation DESC LIMIT 1";
if($query_run= mysqli_query($querycount)){
while($query_rowcount = mysql_fetch_row($query_run)) {
$res=print_r($query_rowcount[0]);
file_put_contents("http://mywebsite/public_html/CodeIgniter/lastinsert.txt",$query_rowcount[0]);
echo $crc2= (int)file_get_contents('http://mywebsite/public_html/CodeIgniter/lastinsert.txt' );
if($crc1!="" && $query_rowcount[0]>$crc1){
$queryres="SELECT distinct push.title as 'title' from relations join etablissement push on ref_local=push.id where id_relation>".$crc1."";
if($query_run1= mysqli_query($queryres)){
$gcmMylocation = array();
while($query_row1 = mysql_fetch_array($query_run1)) {
$gcmMylocation[]=$query_row1['title'];
$result=print_r($query_row1);
foreach ($gcmMylocation as $value) {
echo $query = "SELECT regId FROM users where company='".$value."'";
if($query_run = mysqli_query($query)) {
$gcmRegIds = array();
while($query_row = mysql_fetch_assoc($query_run)) {
array_push($gcmRegIds, $query_row['regId']);
//$res=print_r($query_row);
}
}
etablissement push on ref_relation=push.id join etablissement mypush on ref_local=mypush.id where id_relation>857 and mypush.title='maison' order by ref_local";
echo $query_title="SELECT push.title as 'title' from relations join etablissement push on ref_relation=push.id join etablissement mypush on ref_local=mypush.id where id_relation>".$crc1." and mypush.title='".$value."'";
if($query_run_tit= mysqli_query($query_title)){
while($query_row_tit = mysql_fetch_array($query_run_tit)) {
$gcmMylocationtit[]=$query_row_tit['title'];
foreach ($gcmMylocationtit as $valuetit) {
$pushMessage=$valuetit;
if(isset($gcmRegIds)) {
$message = array('m' => $pushMessage);
$pushStatus = sendPushNotification($gcmRegIds, $message);
}
}
}
}
}
}
}
}
}
file_put_contents("http://mywebsite/public_html/CodeIgniter/newinsert.txt",$query_rowcount[0]);
}

Related

Post a not empty file to Dspace with Curl

I am posting a file to dspace through dspace api, I am doing this with an action in php. With postman I send a form-data with the file, filename, bitstream id and token.
I am getting the temp file path with $_FILES and sending it by postfields curl option, but the file uploads empty, or with weird data, without its content.
public function actionSubirBitstream()
{
if (Yii::$app->request->isPost) {
$body = $_POST;
$prefijo = '/rest/items/';
$host = ConfiguracionDspace::find()->where("clave='host'")->one()->valor;
$puerto = ConfiguracionDspace::find()->where("clave='puerto'")->one()->valor;
$token = $body['token'];
$id = $body['id'];
$final = '/bitstreams';
$name = $body['name'];//nombre del archivo sin espacios
$params = "?name=$name";
$url = $host . ':' . $puerto . $prefijo . $id . $final . $params;
$file = $_FILES['file']['tmp_name'];
//$path = $body['path'];
//$file = new CURLFILE("$path");
$headers = array("Content-Type: application/json", 'Accept: application/json', "rest-dspace-token: " . $token);
$curl = curl_init($url);
$options = array(
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array('file'=> $file,'name' => $name),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers
);
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
}
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('photo'=> new CURLFILE('/C:/Users/pfile3.jpeg'),'name' => $name),
this is the example of uploading a file with form key as photo and another key name
in your screen shot you have id and token in form data but in code its not inside POSTFIELDS
in postman you can generate equalent code of request in any language , try generating the PHP equalent code and see if it works:
To do this just click code link under the request url after saving the request
I already post the file by the $_FILE variable, But what I needed to do was to use CURLFILE to create a new file, and give it the temp path of the file in the server
$file = $_FILES['file']['tmp_name'];
and this in the postfields
CURLOPT_POSTFIELDS => array('file'=> new CURLFile("$file"),'name' => $name),
Thanks for the help friends
Here the code:
public function actionSubirBitstream()
{
if (Yii::$app->request->isPost) {
$body = $_POST;
$prefijo = '/rest/items/';
$host = ConfiguracionDspace::find()->where("clave='host'")->one()->valor;
$puerto = ConfiguracionDspace::find()->where("clave='puerto'")->one()->valor;
$token = $body['token'];
$id = $body['id'];
$final = '/bitstreams';
$name = $body['name'];//nombre del archivo sin espacios
$params = "?name=$name";
$url = $host . ':' . $puerto . $prefijo . $id . $final . $params;
$file = $_FILES['file']['tmp_name'];
//$path = $body['path'];
//$file = new CURLFILE("$path");
$headers = array("Content-Type: application/json", 'Accept: application/json', "rest-dspace-token: " . $token);
$curl = curl_init($url);
$options = array(
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array('file'=> new CURLFile("$file"),'name' => $name),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers
);
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
}
The posted files with the rigth size(finally)
The form data I send by Postman, no need to use the field path

Got unauthorized client error while refreshing access token

Unauthorized client error while calling API https://www.googleapis.com/oauth2/v4/token
$client_id = 'xxx';
$refresh_token='xxx';
$client_secret='xxx';
function GetRefreshedAccessToken($client_id, $refresh_token, $client_secret)
{
$url_token = 'https://www.googleapis.com/oauth2/v4/token';
$curlPost = 'client_id=' . $client_id . '&client_secret=' . $client_secret . '&refresh_token='. $refresh_token . '&grant_type=refresh_token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = json_decode(curl_exec($ch), true);
print_r($data);
exit;
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($http_code != 200)
{
throw new Exception('Error : Failed to refresh access token');
}
return $data;
}
add_action('wpcf7_before_send_mail', 'save_application_form');
function save_application_form($wpcf7) {
$client_id = 'xxx';
$refresh_token='xxx';
$client_secret='xxx';
global $wpdb;
$wpcf7 = WPCF7_ContactForm :: get_current() ;
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$submited = array();
$submited['title'] = $wpcf7->title();
$submited['posted_data'] = $submission->get_posted_data();
$uploaded_files = $submission->uploaded_files();
$cf7_file_field_name = 'file-846';
$image_location = $uploaded_files[$cf7_file_field_name];
$test = pathinfo($image_location);
}
GetRefreshedAccessToken($client_id, $refresh_token, $client_secret);
$token = GetRefreshedAccessToken($client_id, $refresh_token, $client_secret);
$url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=media";
$headers = array(
"Content-Type: image/jpeg",
"Authorization: Bearer ".$token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
//$data1 = '#'.realpath('./metadata.json').';type=application/json;charset=UTF-8';
//$data2 = '#'.realpath('./test.csv').';type=text/csv';
$postdata = array(
'file' => file_get_contents($image_location)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$output = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $output;
}
}
I want to refresh auth token automatically. But I got error unauthorized client error while calling API. Is it possible to upload media file on someones's google drive? For that I need refesh token. So It should not asking login again and again

Error: Skin null does not exist

From my controller, I'm trying to make a post request to https://test.adyen.com/hpp/select.shtml but I continue receiving this error
Error: Skin null does not exist
This is part of my code
$paramsRequest = array (
'merchantReference' => 'reference',
'paymentAmount' => 10,
'currencyCode' => 'EUR',
'shipBeforeDate' => '2017-11-29T13:42:40+1:00',
'skinCode' => 'f8My4RJC',
'merchantAccount' => 'TestNL076',
'sessionValidity' => '2017-11-29T13:42:40+1:00',
'merchantReturnData' => 'string',
'shopperEmail' => 'shopper#gmail.com',
);
$hcma = '8382E2...';
$paramsRequest['merchantSig'] = Util::calculateSha256Signature($hcma, $paramsRequest);
return $this->handlePostRequest($paramsRequest, 'https://test.adyen.com/hpp/select.shtml');
and to handle the request
protected function handlePostRequest($data, $url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERPWD, 'admin' . ":" . 'password');
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $result;
}
Are you trying to perform a lookup of available payments? If so, use the end point.
https://test.adyen.com/hpp/directory.shtml
Otherwise the select.shtml an endpoint to redirect shopper's browser when you want them to enter in information or send them to their bank. Here's link to adyen's local payment method docs.

Standard Json objects for different push notification services

Is there actually a required json object which each push service requires. For example I always see the following in every tutorial:
Android:
var notification = "{\"data\":{\"msg\":\"Breaking " + category + " News!\"}}";
Apple:
var alert = "{\"aps\":{\"alert\":\"Breaking " + category + " News!\"}}";
Do these structures have to be kept? or can I send my own custom objects down to the phone?
I think your question is quite specific to certain platform you are trying to send the Push Notification. If you want to just implement the GCM on standard Android Native App.. You can just embed the code in your server implementation.
public function send_notification($registatoin_ids, $message) {
// include config
include_once './config.php';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
So the answer to your question is No.

Can't create a Folder using BOX API v2.0

function post($url, $params, $headers) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$parent_id = 0;
$name = 'examplefolder';
$params = array('name' => $name => json_encode(array('id' => $parent_id)));
$resp = post('https://api.box.com/2.0/folders', $params, array('Authorization: Bearer ' . $ACCESS_TOKEN_HERE));
When this code executes I got this error in Json format
{"type":"error","status":400,"code":"bad_request","context_info":{"errors":[{"reason":"missing_parameter","name":"parent","message":"'parent' is required"},{"reason":"missing_parameter","name":"name","message":"'name' is required"}]},"help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Bad Request","request_id":"204107642652b93aceca050"}
Is there anything I'm missing?
Additional note.
I even tried to make the params a query string like this.
name=examplefolder&parent={"id":0}
and this gives me a different error
{"type":"error","status":400,"code":"bad_request","context_info":{"errors":[{"reason":"invalid_parameter","name":"entity-body","message":"Invalid value 'name=examplefolder&parent={\"id\":0}'. Entity body should be a correctly nested resource attribute name\/value pair"}]},"help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Bad Request","request_id":"154368831052b93bb027779"}
I'm not totally familiar with this language syntax, but it looks to me like the parent object is not declared in your POST body. You need to be sending this:
{"name":NAME, "parent":{"id":"0"}}
You might be able to accomplish that by changing your $params like so:
$params = array('name' => $name, 'parent' => json_encode(array('id' => $parent_id)));
Also, per the documentation the $parent_id should be serialized as a string, not as an integer.
{"name":NAME, "parent":{"parent_id":"0"}}
This worked for me when I was working Angular 1 - angular file upload module.
However it is different with angular2, still working on..
vm.uploader.onBeforeUploadItem = function (item) {
if (!vm.parentFolderId || item.isVersionUpload) return;
item.headers = { Authorization: 'Bearer ' + vm.userToken };
item.formData = {
name: item.file.name,
parent: {
parent_id: vm.parentFolderId
}
};
}