How to create an ad using Facebook Marketing API for PHP? - facebook-php-sdk

I followed https://developers.facebook.com/docs/marketing-api/buying-api#ad-creative but it didn't work. I stuck at creating AdImage.
Api::init($facebook_appid, $facebook_appsecret, $facebook_accesstoken);
$image = new AdImage(null, $facebook_accountid);
$image->{AdImageFields::FILENAME} = '/path/to/file.jpg';
$image->create();
echo 'Image Hash: ', $image->{AdImageFields::HASH}, PHP_EOL;
I got:
$parent_id as a parameter of constructor is being deprecated, please try not to use this in new code.
But assigning account id to a field doesnt work either:
Api::init($facebook_appid, $facebook_appsecret, $facebook_accesstoken);
$image = new AdImage();
$image->{AdImageFields::ACCOUNT_ID} = $facebook_accountid;
$image->{AdImageFields::FILENAME} = public_path('demo/banners/coworking-300x250.png');
$image->create();
echo 'Image Hash: ', $image->{AdImageFields::HASH}, PHP_EOL;
I got:
A parent ID is required.
-- UPDATE --
The following should work for images:
Api::init($facebook_appid, $facebook_appsecret, $facebook_accesstoken);
$account = new AdAccount($facebook_accountid);
$image = $account->createAdImage([], [
AdImageFields::FILENAME => public_path('demo/banners/coworking-300x250.png'),
]);
The current problem is - how to create account for testing...

i have same problem with you. My problem solved by using setParentId() function from AbstractCrudObject.php file.
Do change your code like this, hope it helps
$image = new AdImage();
$image->setParentId("act_somenumber");

Related

SwiftMailer setBody with database

I have a problem to send a mail, since symfony 2.8 with SwiftMailer.
Before, I used this method :
$body = $this->twig->render($email->getCorp());
With an external service, but now I have this error :
Unable to find template "<div>...template in database..."
I've tried this too, and I've got the same error :
$email = $em->getRepository('BoAdminBundle:Email')->find(3);
$body = $this->render($email->getCorp());
$message = Swift_Message::newInstance()
->setSubject('Subject')
->setFrom('mail#mail.com')
->setTo($insti->getEmail())
->setContentType("text/html")
->setBody($body);
$this->get('mailer')->send($message);
Thanks for your help :) !
EDIT :
$parametersFinal = array_merge($replaces, $replacesInsti); // $replacesInsti = array the client's informations
$body = $this->twig->render($email->getCorp(), $parametersFinal);
How can I insert this "$parametersFinal" in the body ?
According to your comment, the HTML exists by calling the getCorp() function, in which case your code should instead be:
$email = $em->getRepository('BoAdminBundle:Email')->find(3);
$message = Swift_Message::newInstance()
->setSubject('Subject')
->setFrom('mail#mail.com')
->setTo($insti->getEmail())
->setContentType("text/html")
->setBody($email->getCorp())
;
$this->get('mailer')->send($message);
The render() function expects the name of a template to render as its first argument, and an optional array of parameters to pass to that template.

SilverStripe Image DataObject not available after creation

I have this code that creates a new Image based on a new file added to the filesystem but not yet in the DB:
$image = Image::create();
$image->Filename = 'assets/Art/e3434cc7-d348-491a-9dc8-325af3d9086d.jpg';
$image->write();
$images = Image::get();
$image = $images->last();
$vd = new ViewableData();
$ad = new ArrayData(array(
'Image' => $image
));
$strHTML = $vd->customise($ad)->renderWith('Art');
Art.ss contains only $Image.SetWidth(100)
Ignoring the fact the query doesn't look up by ID or whatever... why is the image only rendered into $strHTML if I retrieve the image from the DB after creating? If I delete the code below, $strHTML is empty:
$images = Image::get();
$image = $images->last();
There is a setter available so using that alone may help, but you also may need to assign the ID of the parent folder to the object:
$pFolder = Folder::find_or_make('Art');
if ($pFolder) {
$image->setParentID($pFolder->ID);
$image->setFilename('assets/Art/e3434cc7-d348-491a-9dc8-325af3d9086d.jpg');
}

File path without domain name from wp_get_attachment_url()

wp_get_attachment_url() process full file path like
http://example.com/wp-content/uploads/2014/12/aura.mp3
I want the url without http://example.com/
So, I want above example as wp-content/uploads/2014/12/aura.mp3 instead of http://example.com/wp-content/uploads/2014/12/aura.mp3. How to do it?
You can really easily explode it by / and then take the part with index 3. Example
$url = wp_get_attachment_url(id); //id is file's id
$urllocal = explode(site_url(), $url)[1]; //output local path
Here is the WordPress way using WordPress functions (avoid hacking):
$fullsize_path = get_attached_file( $attachment_id ); // Full path
$filename_only = basename( get_attached_file( $attachment_id ) ); // Just the file name
WordPress has tons of functions, so first try to find the function on the docs: https://developer.wordpress.org/reference/functions/get_attached_file/
You can use PHP's function explode.
Here is the code:
<?php
$image_url = wp_get_attachment_url( 9 ); //ID of your attachment
$my_image_url = explode('/',$image_url,4);
echo $my_image_url[3];
?>
You can implode your entire url on / and array_slice from the end, then implode it back in on /.
$url = wp_get_attachment_url($item->ID); //id is file's id
$url_relative = implode(array_slice(explode('/', $url),-3,3),'/');
//Returns: 2019/08/image.jpg
That way if your WordPress is on a subdomain or localhost or the images are on S3 it won't crash.

prestashop user login integration

I have to integrate PrestaShop 1.5 with pre-existing symfony application.
Through webservices, I can keep the databases in sync so a user can perform login with the same data on both PrestaShop and application software.
Now I want to to ensure that logging in application, the user is automatically logged in the PrestaShop platform.
Can you help me?
I don't know if you're still searching for a solution but there is a way actually.
DO MAKE SURE IT IS A SECURE LOGIN.
Since you're giving access to all prestashop data do make sure the login is very secure. I've been able to recreate it with PHP I think that with some additions you're able to recreate it the way you want it. See it as a guideline.
To create a login system by using the prestashop webservice you'll need three things
Access through webservice to the customers table
The COOKIE_KEY, defined in app/config -> parameters.php:: 'cookie_key' => '12321test';
Some expierence with PHP
The first thing is to get the customers table from the webservice.
// code placeholder
require_once('./../PSWebServiceLibrary.php');
/**
* get information from PrestaShop
*/
$webService = new PrestaShopWebservice($url, $key, $debug);
$COOKIE_KEY = 'CookieKey';
$email = $_REQUEST['email'];
$password = $_REQUEST['password'];
$optUser = array(
'resource' => 'customers',
'filter[email]' => '[' . $email . ']',
'display' => '[id,email,lastname,firstname,passwd]'
);
$resultUser = ($webService->get($optUser));
$json = json_encode($resultUser);
The second and most important thing is to Check the user input
// code placeholder
foreach ($resultUser->customers->customer as $info) {
// Prestashop uses the cookie_key in combination with a salt key. To check the password use the php function: password_verify();
$salt = substr($info->passwd, strrpos($info->passwd, ':') + 1, 2);
$ZCpassword = md5($COOKIE_KEY . $password) . ':' . $salt;
// Check if password comparison is true or false
if (password_verify($password, $info->passwd) == true) {
session_start();
$response = array();
$response['status'] = 'succes';
$response['message'] = "You did it!";
setcookie("userId", $info->id);
header('Content-type: application/json');
echo json_encode($response);
} else {
$response = array();
$response['status'] = 'error';
$response['message'] = 'Wrong password';
header('Content-type: application/json');
echo json_encode($response);
}
}
This is how to reproduce the issue to a working example.
What i've used is setting a cookie and check if it exists!
Hope this helps!

Wordpress XML-RPC post success but don't work with long body

I use code below to post via XML-RPC, it does success. But when I send a long string $bodypost, my post doesn't have body content. I test it with html code, it's working, then I remove all space, my $bodypost just have 1 line with about 4000 words, it's not working.
How can I fix it?
<?php
function send_post($titlepost, $bodypost, $categorypost, $keywordspost )
{
require_once("IXR_Library.php.inc");
$encoding='UTF-8';
$client->debug = false; //Set it to false in Production Environment
$title= $titlepost; // $title variable will insert your blog title
$body= $bodypost; // $body will insert your blog content (article content)
$category=$categorypost; // Comma seperated pre existing categories. Ensure that these categories exists in your blog.
$keywords=$keywordspost;
$customfields=array('key'=>'Author-bio', 'value'=>'Autor Bio Here'); // Insert your custom values like this in Key, Value format
$title = htmlentities($title,ENT_NOQUOTES,$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);
$content = array(
'title'=>$title,
'description'=>$body,
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'post_type'=>'post',
'mt_keywords'=>$keywords,
'categories'=>array($category),
'custom_fields' => array($customfields)
);
// Create the client object
$client = new IXR_Client('http://www.domain.com/xmlrpc.php');
$username = "abc";
$password = "abc";
$params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immideately, to save as draft set it as 'false'
// Run a query for PHP
if (!$client->query('metaWeblog.newPost', $params)) {
die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
}
else
echo "Article Posted Successfully";
}
?>
I found how to fix it. This code does not work if my body code is give by source code of default Wordpress editor, but if I change it to CkEditor it's working!

Resources