gpt4 book ai didi

php - CI 函数不适用于外部 php 脚本

转载 作者:可可西里 更新时间:2023-10-31 23:29:18 24 4
gpt4 key购买 nike

我需要从外部 php 脚本运行一个 CI Controller > 方法。外部 php 脚本使用 curl 发送一些值

$url = 'http://domain.com/ci_system/billing/success';

$fields = array(
'ammount' => $extra[0]['ammount'],
'email' => $extra[0]['email'],
'cemail' => $extra[0]['cemail'],
'rcode' => $completeResponse->getResponseCode(),
'message' => $completeResponse->getResponseText()
);
$post = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_exec($ch);
curl_close($ch);

上面的成功函数我必须发送一封电子邮件。 curl 将所有必需的数据发送到成功函数,但它不起作用。当我手动触发此功能时,它会起作用..

Controller >方法

$this->cart->destroy();
$ord = array(
'online' => 1
);
$this->Billing_model->updateOrder($ord, $this->session->userdata('data_o_i'));
//send email to the client
$this->load->library('email');
$config['protocol'] = 'sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$this->email->initialize($config);

$this->email->from('noreply@zxzxzxz.com', 'Dsd Retail New Order');
$this->email->to($this->input->post('omail@gmail.com'));

$this->email->subject('You have a new message for yor order');
$this->email->message($this->input->post('email'));

最佳答案

首先检查您的操作是否正常工作,如果您从 url 调用它,是否会给出预期的行为。

我有以下 ci3 设置。

主机

$ cat /etc/hosts
127.0.0.1 ci3.local

Apache 站点可用

cat /etc/apache2/sites-available/ci3.conf

<VirtualHost *:80>
ServerAdmin email@email.com
ServerName ci3.local
DocumentRoot /home/user/project/ci3

ErrorLog ${APACHE_LOG_DIR}/ci3_error.log
CustomLog ${APACHE_LOG_DIR}/ci3_access.log combined

SetEnv APPLICATION_ENV "development"

<Directory /home/user/project/ci3>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
Allow from all
</Directory>

</VirtualHost>

ci3/.htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

ci3/application/config/routes.php

$route['billing/success'] = 'billing/success';

ci3/application/controllers/Billing.php

<?php
class Billing extends CI_Controller {

public function success()
{
log_message('debug', 'Billing.success');
log_message('debug', $this->input->post('amount'));
}
}

现在如果我请求 http://ci3.local/billing/success从浏览器,它会将以下输出记录到我的日志文件中

DEBUG - 2015-12-07 08:47:34 --> UTF-8 Support Enabled
DEBUG - 2015-12-07 08:47:34 --> Global POST, GET and COOKIE data sanitized
DEBUG - 2015-12-07 08:47:34 --> Billing.success
DEBUG - 2015-12-07 08:47:34 -->
DEBUG - 2015-12-07 08:47:34 --> Total execution time: 0.0019

如何从终端调用它并检查它是否正常工作?

$ curl --data "amount=200" http://ci3.local/billing/success

然后我应该看到我的日志条目如下所示

DEBUG - 2015-12-07 08:49:38 --> UTF-8 Support Enabled
DEBUG - 2015-12-07 08:49:38 --> Global POST, GET and COOKIE data sanitized
DEBUG - 2015-12-07 08:49:38 --> Billing.success
DEBUG - 2015-12-07 08:49:38 --> 200
DEBUG - 2015-12-07 08:49:38 --> Total execution time: 0.0021

如何通过 http 的 CURL 调用此操作?

ci3/curl.php

<?php
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "http://ci3.local/billing/success",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'amount' => '300'
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);

从终端调用 curl.php

php5 curl.php

然后我应该看到我的日志条目如下所示

DEBUG - 2015-12-07 08:53:41 --> UTF-8 Support Enabled
DEBUG - 2015-12-07 08:53:41 --> Global POST, GET and COOKIE data sanitized
DEBUG - 2015-12-07 08:53:41 --> Billing.success
DEBUG - 2015-12-07 08:53:41 --> 300
DEBUG - 2015-12-07 08:53:41 --> Total execution time: 0.0021

关于php - CI 函数不适用于外部 php 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34126625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com