I am sending push notes from a PHP script. They are received twice on all my devices, so this is not a question of a wrong setup in one of my devices. OTOH, if I send a push notice from one of my devices to another, it appears only once.
I cannot figure out why this is happening and hope someone else has the right idea. The script looks ok to me.
<?php
$qs = $_SERVER['QUERY_STRING']; // get query string like "bookid=35954441&status=modify"
if ($qs ==""){
$qs = 'no message';
}
date_default_timezone_set('America/Panama');
$today = date('d/m/Y H:i:s',$_SERVER['REQUEST_TIME']);
$data = array();
$data['type'] = 'note';
$data['title'] = 'Aparthotel Boquete - '.$today;
$data['body'] = "· ". str_replace("&"," \n· ",$qs) ;
$json = json_encode($data);
$url="https://api.pushbullet.com/v2/pushes";
$ch=curl_init();
curl_setopt($ch, CURLOPT_POST, 1) ;
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ##################', 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$Json_object = curl_exec($ch); // this is a JSON string
$result = json_decode(curl_exec($ch),true); // creates an associative 2 dimensional array $booking[i]['key'] where i= booking index
curl_close ($ch);
?>
var_dump of the JSON object shows this among other items:
,"created":1674359389.1863782,"modified":1674359389.1906438,
This could be a hint. Maybe the push note is created (and sent), and then sent again as "modified"? But why? Is any of the CURL options the cause?
I just found the answer myself after several tests. While the script is executed only once, the CURL call was run a second time when I decoded the result:
.....
$Json_object = curl_exec($ch); // <== first execution
$result = json_decode(curl_exec($ch),true); // <== second execution
curl_close ($ch);
Thank you for your hint, ADyson. I am glad I found it fast enough not to waste more people's time. I let it stand anyway, hoping that it may help others to discover similar errors.
Related
I want to send data from server 1 to server 2, first I select necessary data from the database, but how to send data with curl? I understand that I cannot send $result parameter just like in my code, but how should I do this?
My Code server 1:
public function setDivisions(){
$result = $this->conn->query("SELECT *FROM data_divisions");
$ch = curl_init('https://example.com/api.php?siteid='.$this->site_key.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $result);
curl_setopt($ch, CURLOPT_POST, 1);
$response = curl_exec($ch);
print_r($response);
}
Code on server 2:
$array = $_POST['result'];
//loop throw array and insert data into database
you can use it that way.
$ch = curl_init('https://upxxx.cod3fus1ontm.com/curl/json');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode((object)["records" => json_encode($result)]));
$response = curl_exec($ch);
var_dump($response);
on receipt, like this!
$json = file_get_contents("php://input");
$content = json_decode($json, true);
$records = json_decode($content['records'], true);
foreach($records as $record) {
echo $record['id'] . " - " . $record['text'] . "<br/>";
}
remember, that as you did to encode, you will have to do to decode
Come on, php://input returns all raw data after the request's HTTP headers, regardless of content type.
When we do this with file_get_contents (which reads the contents of a file and puts it into a variable of type string), we can read the content that was sent by curl.
Reviewing the code, you can optimize as follows, when sending to the server you placed, I suggested:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode((object)["records" => json_encode($result)]));
you can replace it with:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($result));
it's much simpler, but it's very important to put the query result inside a json_encode
finally, you can access the entire body of the message, through file_get_contents ("php://input") and put it inside a variable, which is nothing more than a JSON of the your query.
for you to understand how the parameters were passed, it is interesting to do the following:
$json = file_get_contents("php: // input");
var_dump($json); <- Here you see the thing as it is.
$records = json_decode($json, true); <- Here you generate an object with the content of json
var_dump($records);
With that, I think that solves the situation.
on server 1
$result = "result=".base64_encode($result)
curl_setopt($ch, CURLOPT_POSTFIELDS, $result);
...
on server 2
$array = base64_decode($_POST['result']);
I am trying to get additional parameters from Emma's API using php. If anyone can point me in the right direction, I would greatly appreciate it. I know that my problem is terminology and thnk I am struggling with the basics and just need to "get over the hump."
I have been able to successfully test getting the basic fields that Emma provides by using get curl https://api.e2ma.net/account_id/mailings and --header 'Authorization: Basic {{public_api_key}}:{{private_api_key}}'
My problem is that according to their documentation (url below), I should be able to include extra parameters (I need to get the with_html_body parameter).
http://api.myemma.com/api/external/mailings.html
The code below gives me the following error:
Error getting member: {"error": "Unable to parse JSON request"}
I know the $data array is the problem.
Any idea if this is my only problem?
I figure I need to pull in the with_html_body field as an array and then CURLOPT_POSTFIELDS the variable ($data).
Amy I way off base?
Thanks in advance for any help pointing me in the right direction.
<?php
// Authentication Variables
$account_id = "1111111";
$public_api_key = "99999999";
$private_api_key = "99898989";
// Set URL
$url = "https://api.e2ma.net/" . $account_id . "/mailings";
// Open connection
$ch = curl_init();
$data = array('with_html_body' => 'true');
// Make the curl call
curl_setopt($ch, CURLOPT_USERPWD, $public_api_key . ":" . $private_api_key);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$head = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//curl_close($ch);
// check for errors
if($http_code > 200) {
print "Error getting member:\r\n";
print_r($head);
} else {
/*
print "Member information:\r\n";
print_r($head);
*/
$decoded = json_decode($head, true);
echo "<pre>";
print_r($head);
echo "</pre>";
/*
*/
}
curl_close($ch);
?>
Typical me, I over-complicated the issue!
All I needed to do was adjust the $url to below.
$url = "https://api.e2ma.net/" . $account_id . "/mailings?with_html_body=true";
Yes, I feel like an idiot!
But making progress.
Now to put this code in a better form to use on a project.
This example only displayed a blank page for me.
This one did as well.
I've got the latest version of PHP and cURL set up properly, as far as I know so there shouldn't be any problem at that end. I'd prefer JavaScript to retrieve products but I'm open minded.
I happen to not be highly skilled, but I'd like to get my foot in the door.
edit: I will show you the code that doesn't work, and the error it is giving me.
<?php
// Your developer key
$cj_id = "My ID - omitted for privacy.";
// Your website ID
$website_id = "Also removed for privacy.";
// Keywords to search for
$keywords = "credit+card";
// URL to query with cURL
$url = "https://product-search.api.cj.com/v2/product-search?website-id=$website_id&keywords=$keywords";
// Initiate the cURL fetch
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// Send authorization header with the CJ ID. Without this, the query won't work
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$cj_id));
$result = curl_exec($ch);
// Put the results to an object
$resultXML = simplexml_load_string($result);
// Print the results
print "<pre>";
print_r($resultXML);
print "</pre>";
?>
Now, this is the error that it's giving me.
SimpleXMLElement Object
(
[error-message] => Invalid Key provided. Valid keys are: advertiser-ids, advertiser-sku, currency, high-price, high-sale-price, isbn, keywords, low-price, low-sale-price, manufacturer-name, manufacturer-sku, page-number, records-per-page, serviceable-area, sort-by, sort-order, upc, website-id
)
You have a error in your URL, try this:
$url = "https://product-search.api.cj.com/v2/product-search?website-id=$website_id&keywords=$keywords";
instead of :
$url = "https://product-search.api.cj.com/v2/product-search?website-id=$website_id&keywords=$keywords";
<?php
echo '<pre>';
$url='https://product-search.api.cj.com/v2/product-search?website-id=your-id-key-here&advertiser-ids=4415206&records-per-page=999&serviceable-area=US';
$CJ_KEY='0085eb59c8928f028ba5b27bccfe17cdd20cf4e9079b977b2cc6df72752abab9205676a2f7ee67befe9dccab85f656ef46aba49e500faccbf75dfc6e03f655334d/00848a3f9bf0e13525bce27f008d6245c3e42ae80f2d80a8d9d2220807ca386f4b10146cbbcfff06aafb5e49c03a3318213389dee7861abb2dd7229470390a89c9';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, FAlSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$CJ_KEY));
$curl_results = curl_exec($ch);
$xml = simplexml_load_string($curl_results);
var_dump($xml);
// Loop Insert Product to database
echo '<pre>';
// if you no set: records-per-page=999, default get 50 products latest
// advertiser-ids=4415206 is Id of Advertiser in CJ, you can replace other id ,
Hope helpful for you , good luck !
?>
I have been working with Jira API and have seen inconsistent results for my requests. Sometimes it works and sometimes it doesn't. Last week I was able to post attachments to issues just fine, but now an old problem occurred: the names of the attachments contain the whole path of the posted file, hence the attachments can't be opened. I use json representation to post files:
$array = array("file"=>"#filename");
json_encode($array);
...
This gets the file posted but the problem is when it's posted the file names in JIRA are like this:
/var/www/user/text.text
Needless to say it can't be opened in JIRA. I had this problem before, then it suddenly disappeared, now it occurred again. I don't really get it. By the way I am not using curl for this request even though it might be recommended in the documentation.
I realize this question is somewhat old but I had a similar problem. It seems Jira doesn't necessarily trim the filename as expected. I was able to fix it with the following. If you're using PHP >= 5.5.0:
$url = "http://example.com/jira/rest/api/2/issue/123456/attachments";
$headers = array("X-Atlassian-Token: nocheck");
$attachmentPath = "/full/path/to/file";
$filename = array_pop(explode('/', $attachmentPath));
$cfile = new CURLFile($attachmentPath);
$cfile->setPostFilename($filename);
$data = array('file'=>$cfile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
$ch_error = curl_error($ch);
if ($ch_error){
echo "cURL Error: $ch_error"; exit();
} else {
print_r($result);
}
For PHP <5.5.0 but > 5.2.10 (see this bug):
$data = array('file'=>"#{$attachmentPath};filename={$filename}");
Yes, I filed an issue on this at https://jira.atlassian.com/browse/JRA-30765
Adding attachments to JIRA by REST is sadly not as useful as it could be.
Interesting that the problem went away - perhaps you were running your script from a different location?
Obviously I am not using the right keywords in my searches or am not understanding what others have written in blogs or forums, et al.
Looking for information on passing data between two servers. The data will be preferably contained within a JSON array.
If you know of someone who has written a blog fully encompassing this I would really like a chance to read it. Otherwise could you offer some thoughts?
More detailed:
As a user visits a page a PHP function will be called and some data will be "packaged up" in a JSON array and then a POST command to the other server. The second server after receiving the POST will do some processing and then "package up" some data and return it in a JSON array. Then the user will be presented with the results.
With the following I am receiving a HTTP 200 response. Just no data.
SITE 1:
$data_string = json_encode(array('user_id'=>123));
$ch = curl_init('http://site2.dev/retrieve');
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);
$status = curl_getinfo($ch);
curl_close($ch);
$result = json_decode($result);
SITE 2:
public function retrieve() {
return json_encode(array('some'=>'456'));
}
The Json array was chosen as it can be encrypted and yes HTTPS will be used in the final environment.
Both servers will have Laravel 4 as the PHP Framework.
Thank you for your thoughts and comments.
Add this:
curl_setopt($ch, CURLOPT_HEADER, 0);
and put quotes on content-length:
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Content-Type: application/json','"Content-Length: ' . strlen($data_string) . '"'));