I'm trying to get the error message returned by the API in my function to be shown in the controller, but it is returning null even if I set the error variable myself, i.e: $error = 'Error Message'
Function:
class ImportexportDomains {
public function add($input) {
$error = $data['message'];
return $error;
}
}
Controller:
public function store() {
$input = array_except(Input::all(), '_method');
$addnew = new ImportexportDomains;
$addnew->add($input);
dd($addnew);
}
$addnew
ImportexportDomains {#285 ▼
+error: null
}
public function add($input) {
$error = $data['message'];
return $error;
}
In the function above $input was never used, Whatsoever you pass later as parameter in that function will do nothing.
public function add($input) {
$error = "";
if($input == ""){
$error = "Not valid input";
}
return $error;
}
This might not be exactly what you want, but you can get idea from that.
You code have several errors. Looks like you didn't understood how a class and object works.
ImportexportDomains add() isn't a function. It's called method.
$data variable does not exists on add method context. (in fact, it doesn't exist anywhere)
You are doing dd() on the object, not on the add method response.
I know that you want to learn by trying yourself, but there's a nice community called Laracasts, that have some very well explained screencasts.
This two series will help you a lot to understand everything!
https://laracasts.com/series/php-for-beginners
https://laracasts.com/series/laravel-5-from-scratch
You need to pay a month to have access to the videos, but it's only $8!!!
Method
class ImportexportDomains {
public $error;
public function add($input) {
$userid = Settings::where('acc_id', Auth::user()->acc_id)->where('setting', 'resellerclub_id')->value('value');
$apikey = Settings::where('acc_id', Auth::user()->acc_id)->where('setting', 'resellerclub_key')->value('value');
$cust_email = Client::where('id', $input['client_id'])->value('email');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://httpapi.com/api/customers/details.json?auth-userid=$userid&api-key=$apikey&username=$cust_email");
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$httpResponse = curl_exec($ch);
$data = json_decode($httpResponse, true);
if (isset($data['status'])) {
$error = $data['message'];
}
return $this->error = $error;
Controller
public function store() {
$input = array_except(Input::all(), '_method');
$addnew = new ImportexportDomains;
$addnew->add($input);
echo $addnew->error;
}
Related
We have a website that posts data to another website, once that data is send we include a php file. after the php file is included the var_dump doesn't give back the required information ("data received"). I thought it might have been curl at first but when I tried adding the data to a database it didn't do anything either. So by now I asume the code just doesn't continue. when I echo the 'received data' before the getData function it will display the echo if I add it behind the getData function it doesnt display.
Website1:
if(isset($_POST['password'])) {
$password = sha1($_POST['password']);
}
$post = [
$password,
];
$ch = curl_init("http://localhost/JoomlaToJoomlaPlugin/WebsiteOne/plugins/system/controlplugin/encrypted.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
}
Website2:
function __construct() {
$items = null;
if(isset($_POST)) {
$items = $_POST;
}
$this->pass = $items[0];
if(Encrypted::checkHash()) {
echo 'received data!';
Encrypted::getData();
echo 'THIS DOES NOT DISPLAY!!!!!!!!';
Encrypted::sendDatabackTest();
} else {
echo 'error';
}
}
private function getData() {
require_once(JPATH_BASE .DS.'libraries/cms/version/version.php');
echo JVersion::getShortVersion();
Hi I'm new to php and postmark and am trying to get a form submission set to my email. I have the email working however I can't get it to show the header("Location: thanks.php) page. Any help would be greatly appreciated. Thanks.
require("postmark.php");
$postmark = new Postmark("API KEY","calvin.hemington#example.com","$email");
if($postmark->to("calvin.hemington#example.com")->subject("Mission Woodshop | " . $name)->plain_message($email_body)->send()){
exit;
}
header("Location: thanks.php");
exit;
<?php
/**
* This is a simple library for sending emails with Postmark created by Matthew Loberg (http://mloberg.com)
*/
class Postmark{
private $api_key;
private $data = array();
function __construct($apikey,$from,$reply=""){
$this->api_key = $apikey;
$this->data["From"] = $from;
$this->data["ReplyTo"] = $reply;
}
function send(){
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
"X-Postmark-Server-Token: {$this->api_key}"
);
$data = $this->data;
$ch = curl_init('http://api.postmarkapp.com/email');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$return = curl_exec($ch);
$curl_error = curl_error($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// do some checking to make sure it sent
if($http_code !== 200){
return false;
}else{
return true;
}
}
function to($to){
$this->data["To"] = $to;
return $this;
}
function subject($subject){
$this->data["subject"] = $subject;
return $this;
}
function html_message($body){
$this->data["HtmlBody"] = "<html><body>{$body}</body></html>";
return $this;
}
function plain_message($msg){
$this->data["TextBody"] = $msg;
return $this;
}
function tag($tag){
$this->data["Tag"] = $tag;
return $this;
}
}
Presumably $postmark->send() returns true when it works. Your if/then statement says 'exit when the send succeeds'.
If you move the header() call into the if/then it should work as expected. You'll also want to handle the case where the $postmark->to call fails, possibly redirect to error page at that point.
It may be easier to use our new officially supported lib which gives full details on responses for API calls. http://developer.postmarkapp.com/developer-official-libs.html#php
I'm in need of some help with this insane issue i'm having
I have a class i made that i'm using Curl to obtain data from a URL and with that information $_GET's each variable a value.
So i have the class and i'm using a new file to get the $name of the item posted in a new function but every-time i put the phrased variable in the new function i get NULL here is my class in-short
class AppStore {
public $name;
public function getData() {
$requestUrl = self::APPSTORE_LOOKUP_URL . $this->appIdentity;
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_URL, $requestUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $this->iTunesUserAgent);
curl_setopt($ch, CURLOPT_ENCODING, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
$errno = curl_errno($ch);
curl_close($ch);
$parsedResponse = json_decode($response, true);
if ($parsedResponse['resultCount'] == 1)
{
$parsedResponse = $parsedResponse['results'][0];
$this->name = $parsedResponse['trackName'];
require_once('func.ini.php'); // new function to save images etc
} else {
throw new AppStoreService_Exception("mInvalid data returned from the AppStore Web Service. Check your app ID, and verify that the Appstore is currently up.");
}
}
}
// This is the func.ini.php code
echo $parsedResponse['trackName']; // Works here output equals a Name like Baraka etc
function save_image($img){
$dirs = "data/Apps/".$name; // Gets the name of the ID in the appIdentity so we can use CURL to download images and move the images to new folder else it'll fail and won't move
$path = $dirs."ScreenShots";
$fullp = basename($img);
$fullpath = "$path/$fullp";
$something = $dirs.'ScreenShots'
var_dump($parsedResponse['trackName']); // Null same if i echo it i get a white page etc
/* Sample Code
$ch = curl_init ($img);
curl_setopt($ch, CURLOPT_URL,$img);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
*/
}
Basically if you can see if I try to add my class's variable to get the Name of the appIdentity in the new function, save_image it'll be null and i'm not sure how I'll be able to return the $this->name ($parsedResponse['trackName']) to be global for all use in new functions etc since my class is using the public getData() then the $this->name (getData()->name)
this only works out side any functions so if there is a function, the variable set to name will only have value above the function(s) i hope this is understandable.
Not sure if i need to make a return in the $this->name or what because i tried making a new public function in the class below the getName() and that comes out null as well thus i can't set public variables in a = statement because it'll catch(e) from try{} asking it expects a T_FUNCTION than T_VARIABLE i.e public $name = $name; // error public $name = 'Test'; // works '{$name}' don't work
class Test {
public $test = 'Help me out please!';
function GetMe() {
echo $this->test;
}
}
$open = new Test;
echo $open->GetMe(); // Outputs "Help me out please"
/* i can't do that with a variable though :( i.g $this->name / $parsedResponse['trackName'] */
If anyone can help me out I'd be much appreciated
Create an instance of your class and run the function that sets the name variable.
// Create an instance of AppName
$app_store = new AppName();
// Run the function
$app_store->getData();
You can then make use of the resulting $name variable created, within other functions outside of your class:
// By Making the instance global
function save_image()
{
global $app_store;
// Show the value of name
echo $app_store->name;
}
OR... by passing it to any functions that you want to make use of it
function save_image2($name_var)
{
echo $name_var;
}
// Called like
save_image2($app_store->name);
I'm trying to use the curl headerfunction opt from a class. I've tried putting the functions inside the class normally, but curl can't find them that way. So I put them inside the function I need it in. Here's the part of code that's applicable:
$ht = array();
$t = array();
function htWrite($stream,$buffer)
{
$ht[] = $buffer;
return strlen($buffer);
}
function tWrite($stream,$buffer)
{
$t[] = $buffer;
return strlen($buffer);
}
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'htWrite');
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'tWrite');
When I put an echo statement in htWrite for the buffer, it echos out just fine. But if I do a print_r statement on $ht later, it says that it's empty. Further investigation says that it's creating its own $ht variable, because if I remove that line, $ht is null according to the function. So what can I do to fix this?
You need to look at how you can specify object methods as callbacks:
class Foo {
public function Bar() {
// do whatever
}
public function Test() {
$ch = curl_init('http://www.google.com');
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'Bar'));
}
}
My Class for download file direct from a link:
MyClass{
function download($link){
......
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_FILE, $File->handle);
curl_setopt($ch,CURLOPT_WRITEFUNCTION , array($this,'__writeFunction'));
curl_exec($ch);
curl_close($ch);
$File->close();
......
}
function __writeFunction($curl, $data) {
return strlen($data);
}
}
I want know how to use CRULOPT_WRITEFUNCTION when download file.
Above code if i remove line:
curl_setopt($ch,CURLOPT_WRITEFUNCTION , array($this,'__writeFunction'));
Then it will run good, i can download that file.But if i use CURL_WRITEFUNCTION option i can't download file.
I know this is an old question, but maybe my answer will be of some help for you or someone else. Try this:
function get_write_function(){
return function($curl, $data){
return strlen($data);
}
}
I don't know exactly what you want to do, but with PHP 5.3, you can do a lot with the callback. What's really great about generating a function in this way is that the values passed through the 'use' keyword remain with the function afterward, kind of like constants.
function get_write_function($var){
$obj = $this;//access variables or functions within your class with the object variable
return function($curl, $data) use ($var, $obj) {
$len = strlen($data);
//just an example - you can come up with something better than this:
if ($len > $var){
return -1;//abort the download
} else {
$obj->do_something();//call a class function
return $len;
}
}
}
You can retrieve the function as a variable as follows:
function download($link){
......
$var = 5000;
$write_function = $this->get_write_function($var);
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_FILE, $File->handle);
curl_setopt($ch, CURLOPT_WRITEFUNCTION , $write_function);
curl_exec($ch);
curl_close($ch);
$File->close();
......
}
That was just an example. You can see how I used it here: Parallel cURL Request with WRITEFUNCTION Callback. I didn't actually test all of this code, so there may be minor errors. Let me know if you have problems, and I'll fix it.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_BUFFERSIZE, 8096);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://blog.ronnyristau.de/wp-content/uploads/2008/12/php.jpg');
$content = curl_exec($ch);
curl_close($ch);
$out = fopen('/tmp/out.png','w');
if($out){
fwrite($out, $content);
fclose($out);
}
Why do you use curl to download a file? Is there a special reason? You can simply use fopen and fread
I have written a small class for it.
<?php
class Utils_FileDownload {
private $source;
private $dest;
private $buffer;
private $overwrite;
public function __construct($source,$dest,$buffer=4096,$overwrite=false){
$this->source = $source;
$this->dest = $dest;
$this->buffer = $buffer;
$this->overwrite = $overwrite;
}
public function download(){
if($this->overwrite||!file_exists($this->dest)){
if(!is_dir(dirname($this->dest))){mkdir(dirname($this->dest),0755,true);}
if($this->source==""){
$resource = false;
Utils_Logging_Logger::getLogger()->log("source must not be empty.",Utils_Logging_Logger::TYPE_ERROR);
}
else{ $resource = fopen($this->source,"rb"); }
if($this->source==""){
$dest = false;
Utils_Logging_Logger::getLogger()->log("destination must not be empty.",Utils_Logging_Logger::TYPE_ERROR);
}
else{ $dest = fopen($this->dest,"wb"); }
if($resource!==false&&$dest!==false){
while(!feof($resource)){
$read = fread($resource,$this->buffer);
fwrite($dest,$read,$this->buffer);
}
chmod($this->dest,0644);
fclose($dest); fclose($resource);
return true;
}else{
return false;
}
}else{
return false;
}
}
}
It seems like cURL uses your function instead of writing to the request once CURLOPT_WRITEFUNCTION is specified.
So the correct solution would be :
MyClass{
function download($link){
......
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_FILE, $File->handle);
curl_setopt($ch,CURLOPT_WRITEFUNCTION , array($this,'__writeFunction'));
curl_exec($ch);
curl_close($ch);
$File->close();
......
}
function __writeFunction($curl, $data) {
echo $data;
return strlen($data);
}
}
This can also handle binary files as well.