I trying to run a task in PHP using gearman I created 2 scripts:
client.php
<?
$mail = array(
'to' => 'test#gmail.com',
'subject' => Hi',
'body' => 'Test message',
);
# Connect to Gearman server
$client = new GearmanClient();
$client->addServer('127.0.0.1', '4730');
# Send message
$client->doBackground('sendmail', json_encode($mail));
worker.php
<?php
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction('sendmail', 'send_mail');
while (1)
{
$worker->work();
if ($worker->returnCode() != GEARMAN_SUCCESS) break;
}
function send_mail($job)
{
$workload = $job->workload();
$data = json_decode($workload, true);
mail($data['to'], $data['subject'], $data['body']);
}
when I run my worker from comand line : php worker.php &
and run my client.php file I get the below error:
GearmanClient::doBackground(): send_packet(GEARMAN_COULD_NOT_CONNECT) Failed to send server-options packet -> libgearman/connection.cc:485
Any help please?
Thanks
Try to change this:
$client->addServer('127.0.0.1', '4730');
to
$client->addServer();
If still no working, try to take a look in the get started page of this tutorial on php. It worked fine for me
You forgot to start gearman by running gearmand -d
Read more about it in the documentation at http://gearman.org/getting-started/
Related
I am using the following PHP script to send an email from my server. I need to send an email to the admin when a new record is created in the DB. So within the same php script that updates the DB, I want to trigger the other script that sends the email.
Problem is no matter what I do, apache will not execute the email script when requested from the web/api.
However, when I run php sendemail.php from the command line it works. Also when I run php updatedb.php which includes the exec('php sendemail.php') also works from the command line (these are all executed with root "ec2-user").
Things I tried:
I checked the disabled functions in php.ini and it is empty, nothing is disabled.
I tried changing file permissions to include 'x' for the apache group still no go.
I tried replacing exec with shell_exec, and include, no luck.
Here is 'sendemail.php':
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
use Aws\Ses\SesClient;
use Aws\Exception\AwsException;
$SesClient = new SesClient([
'profile' => 'default',
'version' => '2010-12-01',
'region' => 'us-west-2'
]);
$sender_email = 'sender#example.com';
$recipient_emails = ['recipient1#example.com','recipient2#example.com'];
$configuration_set = 'ConfigSet';
$subject = 'Amazon SES test (AWS SDK for PHP)';
$plaintext_body = 'This email was sent with Amazon SES using the AWS SDK for PHP.' ;
$html_body = '<h1>AWS Amazon Simple Email Service Test Email</h1>'.
'<p>This email was sent with <a href="https://aws.amazon.com/ses/">'.
'Amazon SES</a> using the <a href="https://aws.amazon.com/sdk-for-php/">'.
'AWS SDK for PHP</a>.</p>';
$char_set = 'UTF-8';
try {
$result = $SesClient->sendEmail([
'Destination' => [
'ToAddresses' => $recipient_emails,
],
'ReplyToAddresses' => [$sender_email],
'Source' => $sender_email,
'Message' => [
'Body' => [
'Html' => [
'Charset' => $char_set,
'Data' => $html_body,
],
'Text' => [
'Charset' => $char_set,
'Data' => $plaintext_body,
],
],
'Subject' => [
'Charset' => $char_set,
'Data' => $subject,
],
],
'ConfigurationSetName' => $configuration_set,
]);
$messageId = $result['MessageId'];
echo("Email sent! Message ID: $messageId"."\n");
} catch (AwsException $e) {
// output error message if fails
echo $e->getMessage();
echo("The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n");
echo "\n";
}
a short version of the updatedb.php file, omitting all transactions:
<?php
exec('php send_email.php', $sendEmail);
require_once 'response.php';
$response = new response();
$response->setHttpStatusCode(201);
$response->setSuccess(true);
$response->addMessage('DB Record Inserted successfully ::: ');
$response->setData($sendEmail);
$response->send();
?>
in the updatedb.php file, if I change the first line to echo exec('whoami') and hit it from the web it works. Which is what I am looking for exactly except that I want to work for php sendemail.php
Environment: AWS EC2 Amazon Linux 2 AMI. PHP 7.2.34
I hope it is clear. I am beginner with linux. Please help. Thanks in advance to all.
Thank you so much #Riz your tip about sudo -u apache php sendemail.php saved my day! I was able to debug on the command line and it turns out that my mistake was with the line require 'vendor/autoload.php'; in my original sendemail.php script I was requiring the file from a directory that did not belong to the apache group. So, once I moved the vendor folder to the same folder as the sendemail.php script file everything worked great!
Lesson learned: Make sure all required/included files belong to the apache group. There was no need to grant apache any execution permissions on any file.
i have a problem with soap class in php. i have write a code to send sms via a sms panel. these codes run correctly on localhost (when run codes by xampp on my pc) but this code don't work when i run them on server. the php versions are same on both of them (localhost and xampp)
<?php
$FORM="30005966371";
$USERNAME="xxxx";
$PASSWORD="12345";
$DOMAIN="0098";
//---- variables ----
$TO="0935xxxxxxx";
$TEXT="test msg";
//-------------------
ini_set("soap.wsdl_cache_enabled", "0");
$sms_client = new SoapClient('http://webservice.0098sms.com/service.asmx?wsdl',array('encoding'=>'UTF-8'));
$parameters['username'] = $USERNAME;
$parameters['password'] = $PASSWORD;
$parameters['mobileno'] = $TO;
$parameters['pnlno'] = $FORM;
$parameters['text']=$TEXT;
$parameters['isflash'] =false;
echo $sms_client->SendSMS($parameters)->SendSMSResult;
?>
when i run above codes on localhost the message sends correctly but when run this code on server the following error returns:
bimehco.ir is currently unable to handle this request.
HTTP ERROR 500
i enabled soap extension in php.ini file on server but it still dont work correctly.
The initialization of the SoapClient class should look as follows when searching for errors.
$wsdl = 'http://webservice.0098sms.com/service.asmx?wsdl';
$options = [
'cache_wsdl' => WSDL_CACHE_NONE,
'exceptions' => true,
'trace' => true,
];
try {
$client = new SoapClient($wsdl, $options);
// do your request stuff here
} catch (SoapFault $fault) {
echo $fault->getMessage();
if ($client instanceof SoapClient) {
echo $client->__getLastRequest();
echo $client->__getLastResponse();
}
}
my soap codes were true and don't have any problem. it was a problem on host.
you can use above codes for soap.
or you can use curve function instead.
I am trying to create a simple PHP webservice as I am a newbie in this track. I decided to develop it using SOAP. I am using WAMP as a server and the problem is that I am unable to run the scripts nor get the WSDL file.
Here's server.php's code:
<?php
//call library
require_once ('lib/nusoap.php');
//using soap_server to create server object
$server = new soap_server;
//register a function that works on server
$server->register('get_message');
// create the function
function get_message($your_name)
{
if(!$your_name){
return new soap_fault('Client','','Put Your Name!');
}
$result = "Hello World ".$your_name .". Thanks for Your First Web Service Using PHP with SOAP";
return $result;
}
// create HTTP listener
$server->service($HTTP_RAW_POST_DATA);
exit();
?>
and here's a screenshot of the run:
Here's client.php's code:
<?php
require_once ('lib/nusoap.php');
//Give it value at parameter
$param = array( 'your_name' => 'Omar');
//Create object that referer a web services
$client = new soapclient('http://localhost/WebServiceSOAP/server.php');
//Call a function at server and send parameters too
$response = $client->call('get_message',$param);
//Process result
if($client->fault)
{
echo "FAULT: <p>Code: (".$client->faultcode."</p>";
echo "String: ".$client->faultstring;
}
else
{
echo $response;
}
?>
and here's a screenshot of the run:
running client.php
plus this error keeps bugging me:
Undefined variable: HTTP_RAW_POST_DATA
can u try this below code
$client = new soapclient('http://localhost/WebServiceSOAP/server.php');
to
$client = new SoapClient(
null,
array(
'location' => 'ADD YOUR LOCATION',
'uri' => 'ADD YOUR WSDL FILE ',
'trace' => 1,
'use' => SOAP_LITERAL,
)
);
You're trying to work with undefined variable $HTTP_RAW_POST_DATA. In PHP7 this hook is removed. You can read here
Instead of that I propose to do it like this:
$server->service(file_get_contents("php://input"));
I'm trying to connect to the twilio for sending SMS. It was working but it stopped. After i returned from holidays.
Well SMS sending is working, but i cant echo the answer from Twilio.
I found out that my php file is working sweet on PHP 5.3 but on 5.6 it is throwing an error. So it has something to do with echoing $client but i dont know whats wrong.
Here is my code:
<?php
// this line loads the library
require dirname(__FILE__) . "../../../includes/Services/Twilio.php";
$account_sid = 'XXX';
$auth_token = 'XXX';
$client = new Services_Twilio($account_sid, $auth_token);
//Get the submitted data
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$mamiMobile = $request -> mamiPhone;
$text = $request -> smsoffer;
$client->account->messages->create(array(
'To' => $mamiMobile,
'From' => "+41798071977", //From Number from Twilio
'Body' => $text
));
//This works on php 5.3 but on 5.6 it is not working!
echo ($client);
;?>
The Error i get in the PHP.log:
[03-Mar-2016 18:18:16 Europe/Zurich] PHP Catchable fatal error: Method Services_Twilio::__toString() must return a string value in /Applications/MAMP/htdocs/angular-bootstrap-admin-web-app-with-angularjs/angular/includes/php/sms_connector_twillio_offer.php on line 34
Twilio developer evangelist here.
In order to echo the response from Twilio you shouldn't be echoing the $client itself, but the response to $client->account->messages->create. Why not try something like:
$response = $client->account->messages->create(array(
'To' => $mamiMobile,
'From' => "+41798071977", //From Number from Twilio
'Body' => $text
));
echo ($response);
In answer to what changed between PHP 5.3 and 5.6, my guess is that something happened between those versions to how $this responds to foreach such that this code no longer works as expected.
Could you raise a bug in the GitHub issues for the twilio-php project so that someone can take a look at it? Thanks!
its works by hit url but in cronjob script not workes.Cannot instantiate non-existent class: soapclient in command prompt.
$wsdl ='********/InvoicingService?wsdl';
$client = new SoapClient($wsdl, array("trace"=> 1,"exceptions" => 0));
$invoicecheck = array("username" => "*****","password" => "*****","invoiceNo" =>"****");
$proxy = $client->getProxy();
$value2 = $client ->checkInvStatus($invoicecheck);
$statusInvoice=$value2->return->responseMessage;
if($statusInvoice=='Paid'){
mail('mahtab46#gmail.com','wsdl check cron mail','paid');
echo 'working';
} else {
echo 'not worked';
}
The cronjob executes the PHP CLI handler. This probably uses a different php.ini that doesn't load the soap extension. Try to have php run a php -i > /tmp/test.txt or something and see if the Soap functionality is in there?