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.
Related
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.
Trying to add some kind of value to each data point so I can send the response (numbers only) to an existing table . I've been searching online and no CURL API response seems to be this simple so I can find an answer. (sorry new to this, don't know the "lingo")
This is the response I've been able to echo on screen.
"{"price": 2049.27, "change_point": -5.76, "change_percentage": -0.28, "total_vol": "1.27M"}"
rest of php below
<html>
<?php
$url = 'https://realstonks.p.rapidapi.com/';
$collection_name = $_REQUEST["stock_name"];
$request_url = $url . '/' . $collection_name;
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'X-RapidAPI-Host: www.xyz.com',
'X-RapidAPI-Key: xxxxxxxxxxxxxxxxxxx',
'Content-Type: application/json']);
$response = curl_exec($curl);
curl_close($curl);
//Uncomment bellow to show data on screen
echo $response . PHP_EOL;
enter code here
//var_dump(json_decode($json));
//var_dump(json_decode($json, true));
// Initiate curl
$ch = curl_init();
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
?>
I'm not much into PHP but here to here. If I'm not mistaken it, I think you want to store $response in <td> element. This resource can help https://www.daniweb.com/programming/web-development/threads/428730/how-to-display-php-variable-in-html
Im using 2ba its API to receive product information which I later on want to store inside my database. I am trying to create a post request for receiving the data I need. This is the request I want to get to working. And this is my code:
postApiData.php
<?php
/**
* Posts API data based on given parameters at index.php.
*/
// Base url for all api calls.
$baseURL = 'https://api.2ba.nl';
// Version number and protocol.
$versionAndProtocol = '/1/json/';
// All parts together.
$url = $baseURL . $versionAndProtocol . $endPoint;
// Init session for CURL.
$ch = curl_init();
// Init headers. Security for acces data.
$headers = array();
$headers[] = "Authorization: Bearer " . $token->access_token;
// Options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
// Execute request.
$data = curl_exec($ch);
// If there is an error. Show whats wrong.
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
echo "<br>";
echo "Error location: postApiData";
exit();
}
// Ends the CURL session, frees all resources that belongs to the curl (ch).
curl_close($ch);
// String to array.
$data = json_decode($data);
?>
index.php
// Specified url endpoint. This comes after the baseUrl.
$endPoint = 'Product/forGLNAndProductcodes';
// Parameters that are required or/and optional for the endPoint its request.
$parameters = [
'gln' => '2220000075756',
'productcodes' => ['84622270']
];
// Get Supplier info
include("postApiData.php");
print_r($data);
exit();
My API key does for sure work since I have done alot of different GET requests already, also im not getting an access denied error.
The error I get with this code is: The requested URL returned error: 500 Internal Server Error
I also receive a "Bad request" 400 error when I remove the curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters)); part
Is there anyone who knows what im doing wrong?
PS: It's not really possible to try this code yourself unless you have a 2ba account with working key's etc.
Okey I fixed it already...
I had to add some extra headers and change the $parameters values like this:
postApiData.php
// Added this above the authorization.
$headers[] = "Connection: close";
$headers[] = "Accept-Encoding: gzip,deflate";
$headers[] = "Content-Type: application/json";
// Removed the http_build_query part.
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
index.php
// Encoded in a json way as asked by 2ba request.
$parameters = json_encode($parameters);
I am new at programming Slash commands in Slack. For one of my commands, I have a username and need to retrieve the user icon URL. I am using PHP to code them.
I was planning on using users.profile.get, since the tutorial here shows that one of the fields returned is the user icon URL.
However, I am trying to find examples on how to make a call to this method and have not found any. Could anybody give me a quick example of the call, including how to send the parameters?
This is how far I got:
$slack_profile_url = "https://slack.com/api/users.profile.get";
$fields = urlencode($data);
$slack_call = curl_init($slack_profile_url);
curl_setopt($slack_call, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($slack_call, CURLOPT_POSTFIELDS, $fields);
curl_setopt($slack_call, CURLOPT_CRLF, true);
curl_setopt($slack_call, CURLOPT_RETURNTRANSFER, true);
curl_setopt($slack_call, CURLOPT_HTTPHEADER, array(
"Content-Type: application/x-www-form-urlencoded",
"Content-Length: " . strlen($fields))
);
$profile = curl_exec($slack_call);
curl_close($slack_call);
I basically have $token and $user_name and need to get the profile picture URL. How do I format $token and $username as $data? Is the call correct?
If anybody recommends doing this a different way, I would appreciate any advice as well.
Thank you so much!
To get data into the right format to post to Slack is pretty straight forward. There's two options (POST body or application/x-www-form-urlencoded).
The query string for application/x-www-form-urlencoded is formatted like a get URL string.
https://slack.com/api/users.profile.get?token={token}&user={user}
// Optionally you can add pretty=1 to make it more readable
https://slack.com/api/users.profile.get?token={token}&user={user}&pretty=1
Just request that URL and you will retrieve the data.
The POST body format will use a similar code to what you have above.
$loc = "https://slack.com/api/users.profile.get";
$POST['token'] = "{token}";
$POST['user'] = "{user}";
$ch = curl_init($loc);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $POST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if ($error = curl_errno($ch)) { echo $error; }
//close connection
curl_close($ch);
echo $result;
I am struggling to understand this error when i am trying to post the data the the 3rd party api database.
I have the $data here which i want to post with the curl and it is in json format.
<?php
$url = 'https://www.yourdomain.com/uploadproducts';
$api_key = 'nMUiKg/oj7xtq40';
$sec_key = 'Nf+NQ2/WyjohqXOF1';
$data = '{"product_sku":"FLAFFA000","product_category":"Fifth Avenue","product_subcategory":"0","product_name":"Love Print Flats","product_url":"http://love-print-flats-857","product_description":"Solid Candy Color Cartoon Printed Big","price":"1850","age_group":"0","product_img":"media/catalog/product/f/l/flaffa0001pin38.jpg,media/catalog/product/f/l/flaffa0001pin38_a.jpg,media/catalog/product/f/l/flaffa0001pin38_b.jpg","product_stock":[{"38":"1.0000"}],"product_discount":"100","discount_start_date":"0","discount_end_date":"0","product_tags":["0"]}';
//var_dump($data);
$data_string ='scapi_key='.urlencode($api_key).'&scsecret_key='.urlencode($sec_key)."&products=".urlencode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if(curl_errno($ch))
{
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
$arr = json_decode($response, true);
echo "<pre>$response</pre>";
echo $arr;
?>
When i m trying to execute this file with the url www.yourdomain.com/test.php i m getting the following error.
{"msg":"200 OK","success":"Successfully added 0","updated":"Successfully updated 0","error":"Errors found 14"}
Array
Then i have tried using the
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
to my code above, and i m getting the following error
{"msg":"API key is missing","success":"Successfully added 0","updated":"Successfully updated 0"}
Array
I have also dumped the json data and have validated the json format,
string(943) "{"product_sku":"FLAFFA000","product_category":"Fifth Avenue","product_subcategory":"0","product_name":"Love Print Flats","product_url":"http://love-print-flats-857","product_description":"Solid Candy Color Cartoon Printed Big","price":"1850","age_group":"0","product_img":"media/catalog/product/f/l/flaffa0001pin38.jpg,media/catalog/product/f/l/flaffa0001pin38_a.jpg,media/catalog/product/f/l/flaffa0001pin38_b.jpg","product_stock":[{"38":"1.0000"}],"product_discount":"100","discount_start_date":"0","discount_end_date":"0","product_tags":["0"]}";
On researching more on this issue, i have gone through the document of curl codes and it say that Error 14 is CURLE_FTP_WEIRD_227_FORMAT (14)
FTP servers return a 227-line as a response to a PASV command. If libcurl fails to parse that line, this return code is passed back.
So as per the error shown, is there any possible solution with the curl error 14 or is there error in the code ? What can be the issue with FTP?
Any little help or hint would help me out.