In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to import ThinkPHP into the third-party class library. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
Third-party class library
The third-party class library refers to other class libraries except ThinkPHP framework and application project class library, which are generally provided by third-party systems or products, such as Smarty, Zend and other systems.
For class libraries imported using autoloading or import methods, the ThinkPHP convention is suffixed with .class.php, and non-such suffixes need to be controlled by the parameters of import.
However, for the third type of library, since there is no such convention, the suffix can only be thought of as php. In order to facilitate the introduction of other frameworks and system class libraries, ThinkPHP specially provides the function of importing third-party class libraries. The third-party class libraries are uniformly placed under the ThinkPHP system directory / Vendor and imported using the vendor method.
Vendor method
Syntax:
Boolenvendor (class,baseUrl,ext)
Parameter description:
Parameter description
Class must represent the class library to be imported, using a namespace.
BaseUrl is optional, indicating the base path of the import. If omitted, the system uses the ThinkPHP system directory / Vendor directory.
Ext is optional, indicating the imported class library suffix. The default is .php.
Unlike the import method, the default import path of the vendor method is the ThinkPHP system directory / Vendor directory, and the default suffix is .php.
Personal experience sharing:
When we want to introduce third-party extensions into ThinkPHP, and third-party extensions are not written in accordance with the ThinkPHP specification, we need to place third-party extensions in the Library/Vendor directory. Of course, for ThinkPHP3.2, the lower version depends on the situation.
Then when you need to use a third-party extension in Controller or function, you can directly use the vendor () method to make references.
Third-party class library directory structure:
Use in the function function:
The first method:
Vendor ('Phpqrcode.phpqrcode')
Copy the code
Copy the code
/ * *
* generate a QR code
* @ paramstring$urlurl connection
* @ paraminteger$size size pure number
* /
Functionqrcode ($url,$size=4) {
Vendor ('Phpqrcode.phpqrcode')
If (strpos ($url,'http') = false) {
$url=' http://'.$url;
}
QRcode::png ($url,false,QR_ECLEVEL_L,$size,2,false,0xFFFFFF,0x000000)
}
Copy the code
Copy the code
The second method:
Require'./ThinkPHP/Library/Org/Nx/class.phpmailer.php'
Require'./ThinkPHP/Library/Org/Nx/class.smtp.php'
Copy the code
Copy the code
/ * *
* send email
* @ paramstring$address the email address to be sent to multiple addresses needs to be written as an array
* @ paramstring$subject title
* @ paramstring$content content
Is * @ returnboolean successful?
* /
Functionsend_email ($address,$subject,$content) {
$email_smtp=C ('EMAIL_SMTP')
$email_username=C ('EMAIL_USERNAME')
$email_password=C ('EMAIL_PASSWORD')
$email_from_name=C ('EMAIL_FROM_NAME')
If (empty ($email_smtp) | | empty ($email_username) | | empty ($email_password) | | empty ($email_from_name)) {
Returnarray ("error" = > 1, "message" = > "incomplete mailbox configuration")
}
Require'./ThinkPHP/Library/Org/Nx/class.phpmailer.php'
Require'./ThinkPHP/Library/Org/Nx/class.smtp.php'
$phpmailer=new\ Phpmailer ()
/ / set PHPMailer to send Email using SMTP server
$phpmailer- > IsSMTP ()
/ / set to html format
$phpmailer- > IsHTML (true)
/ / set the character encoding of the message'
$phpmailer- > CharSet='UTF-8'
/ / set up the SMTP server.
$phpmailer- > Host=$email_smtp
/ / set to "need verification"
$phpmailer- > SMTPAuth=true
/ / set user name
$phpmailer- > Username=$email_username
/ / set password
$phpmailer- > Password=$email_password
/ / set the From field of the header.
$phpmailer- > From=$email_username
/ / set the sender's name
$phpmailer- > FromName=$email_from_name
/ / add recipient address, which can be used multiple times to add multiple recipients
If (is_array ($address)) {
Foreach ($addressas$addressv) {
$phpmailer- > AddAddress ($addressv)
}
} else {
$phpmailer- > AddAddress ($address)
}
/ / set the message title
$phpmailer- > Subject=$subject
/ / set the message body
$phpmailer- > Body=$content
/ / send an email.
If (! $phpmailer- > Send ()) {
$phpmailererror=$phpmailer- > ErrorInfo
Returnarray ("error" = > 1, "message" = > $phpmailererror)
} else {
Returnarray ("error" = > 0)
}
}
Copy the code
Copy the code
The third method:
Directory structure of Alipay Class Library
Vendor ('Alipay.AlipaySubmit','','.class.php')
Note: the default suffix loaded by Vendor is .php
Parameter 1: must indicate the class library to be imported, using the namespace method
Parameter 2: optional, indicating the basic path of the import. If omitted, the system uses the ThinkPHP system directory / Vendor directory.
Parameter 3: optional, indicating the imported class library suffix. The default is .php.
Alipay third party case code:
Copy the code
Copy the code
/ * *
* Jump payment to Alipay
* @ paramarray$order order data must contain out_trade_no (order number), price (order amount), subject (item name title)
* /
Functionalipay ($order) {
Vendor ('Alipay.AlipaySubmit','','.class.php')
/ / get configuration
$config=C ('ALIPAY_CONFIG')
$data=array (
"_ input_charset" = > $config ['input_charset'], / / Encoding format
"logistics_fee" = > "0.00", / / Logistics cost
"logistics_payment" = > "SELLER_PAY", / / Logistics payment method SELLER_PAY (seller pays freight), BUYER_PAY (buyer pays freight)
"logistics_type" = > "EXPRESS", / / Logistics type EXPRESS (express), POST (surface mail), EMS (EMS)
"notify_url" = > $config ['notify_url'], / / Link to receive payment status notifications asynchronously
"out_trade_no" = > $order ['out_trade_no'], / / order number
"partner" = > $config ['partner'], / / partner is obtained from Alipay merchant personal center
"payment_type" = > "1", / / the payment type corresponds to the payment_type parameter of the request, which is returned as is. Just set it to 1.
"price" = > $order ['price'], / / order price is in CNY
/ / "price" = > 0. 01 Maxima / Universe / Price adjustment for testing
"quantity" = > "1", / / price, quantity can replace total_fee. That is, there can be total_fee, there can be no price and quantity;, there can be price, quantity, there can be no total_fee. (I don't get it; all right; just ignore this parameter)
"receive_address" = > '1shipping grammar control / consignee address immediate arrival method can ignore this parameter
"receive_mobile" = > '1handlewaspact / the consignee's mobile phone number can be reached immediately by ignoring it.
"receive_name" = > '1shipping grammar control / the consignee's name can be paid immediately by ignoring it.
"receive_zip" = > '1postal grammar control / consignee postal code immediate arrival method can be ignored.
"return_url" = > $config ['return_url'], / / Page Jump synchronization Notification Page path after Alipay processes the request, the current page automatically jumps to the http path of the specified page in the merchant website.
"seller_email" = > $config ['seller_email'], / / email is obtained from Alipay merchant personal center
"service" = > "create_direct_pay_by_user", / / the interface name is always set to create_direct_pay_by_user
"show_url" = > $config ['show_url'], / / the hyperlink to the product display on the cashier page.
"subject" = > $order ['subject'] / / title of commodity name / transaction title / order title / order keyword, etc.
)
$alipay=new\ AlipaySubmit ($config)
$new=$alipay- > buildRequestPara ($data)
$go_pay=$alipay- > buildRequestForm ($new,'get',' pay')
Echo$go_pay
}
Copy the code
Copy the code
However, when I put PHPMailer in the Vendor directory, it runs well on this machine. Recently, when I uploaded the program to the server, I directly prompted Class'PHPMailer'notfound and then ran it on the local machine. As you can see from the previous blog, I am through vendor ('PHPMailer.class#PHPMailer').
This line of code introduces PHPMailer. Since the prompt cannot find the PHPMailer class, it has not been introduced correctly. Why is that?
After a cursory glance at the source code of the vendor () method, it is found that the vendor () method actually assembles the parameters of the import () method, and then gives it to the import () method to handle. Looking at the source code of the import () method, it is found that in the import () method, the parsing of the parameters passed in above is actually the'.' Replace it with'/', replace'# 'with'.', and baseurl is automatically added by the vendor () method, pointing to the Vendor directory. So the parameters in the above vendor () method are finally parsed into the following directory:
Library/Vendor/PHPMailer/class.PHPMailer.php
The actual directory address of the entry file of PHPMailer is:
Library/Vendor/phpmailer/class.phpmailer.php
The content is all the same! However, I am using a Linux server, so it is strictly case-sensitive, so of course I cannot successfully import this class. The solution is to introduce vendor () into:
Vendor ('phpmailer.class#phpmailer')
In addition, note when using PHPMailer. If PHPMailer uses SMTP to send mail, it needs PHP's support for fsockopen, so we need to modify the disable_functions in php.ini to delete fscokopen, otherwise there will be a running error:
Fsockopen () hasbeendisabled
It can be obtained through the ErrorInfo property of PHPMailer!
These are all the contents of the article "how to import ThinkPHP into third-party libraries". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.