Curl-questions about sending emails and network requests in PHP Loop

Source: Internet
Author: User
Problem 1 previously encountered a problem in another project: when PHP uses foreach to send emails cyclically, the following error occurs: {code ...} the code execution result should be: one email is received in each of the three mailboxes, but the fact is that one email is received in the first mailbox and one email is received in the second mailbox... question 1

I have encountered a problem in other projects before: the following error occurs when PHP uses foreach to send emails cyclically:

Sample Code: $ emails = ['2017 @ qq.com ', '2017 @ qq.com', '2017 @ qq.com ',]; // $ Email is the mail sending class foreach ($ emails as $ email) {$ Email-> send ($ email, 'subobject', 'test ');}

The code execution result should be: one email is sent to each of the three mailboxes, but the fact is:
FirstEmail received 1 Email;
SecondEmail received 2 Email;
ThirdEmail received 3 Email;

Why? Later, the project email was changedAmazon Mail Service, So I didn't think about it.

Question 2

Recently, a similar problem has been encountered in the project: the scheduled script obtains remote URL content in batches. The code is roughly as follows:

$ Sites = ['www .site1.com ', 'www .site2.com', 'www .site2.com ',]; // $ CURL complement foreach ($ sites as $ site) {$ result = $ CURL-> get ($ site); var_dump ($ result );}

I thought it would be to print the return values of the three sites, but the time was not the case. When reviewing the database logs, I found that a lot of data is not correct.
It is confusing to use it in a loop. CURL Are you sure you want to consider concurrency? Cainiao.

Reply content: Question 1

I have encountered a problem in other projects before: the following error occurs when PHP uses foreach to send emails cyclically:

Sample Code: $ emails = ['2017 @ qq.com ', '2017 @ qq.com', '2017 @ qq.com ',]; // $ Email is the mail sending class foreach ($ emails as $ email) {$ Email-> send ($ email, 'subobject', 'test ');}

The code execution result should be: one email is sent to each of the three mailboxes, but the fact is:
FirstEmail received 1 Email;
SecondEmail received 2 Email;
ThirdEmail received 3 Email;

Why? Later, the project email was changedAmazon Mail Service, So I didn't think about it.

Question 2

Recently, a similar problem has been encountered in the project: the scheduled script obtains remote URL content in batches. The code is roughly as follows:

$ Sites = ['www .site1.com ', 'www .site2.com', 'www .site2.com ',]; // $ CURL complement foreach ($ sites as $ site) {$ result = $ CURL-> get ($ site); var_dump ($ result );}

I thought it would be to print the return values of the three sites, but the time was not the case. When reviewing the database logs, I found that a lot of data is not correct.
It is confusing to use it in a loop. CURL Are you sure you want to consider concurrency? Cainiao.

Paste the complete code

Question 1: What mail class library do you use?
$ Mailer-> send ($ emails,...) here $ emails can be imported into the Array
The reason why the number of emails received is incorrect may be related to internal pointers. This is also related to the PHP version. The same explanation applies to question 2.
You can take a look at the instructions on foreach in the official PHP documentation.
In PHP 5, when foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. this means that you do not need to call reset () before a foreach loop.
As foreach relies on the internal array pointer in PHP 5, changing it within the loop may lead to unexpected behavior.
In PHP 7, foreach does not use the internal array pointer.

Problem 1: I guess the cause is that the topic and content of the message are saved during sending. The second call of send is to save one, so that the content is accumulated.
Solution: You can change the email library. Or you can create a new email object in foreach, so that each email object is different.
Problem 2: curl is a synchronous request. A simple pull request can be replaced by the file_get_contents function.

Problems found temporarily:
Your question 1 $ Email class and loop variable $ email is a duplicate name, right?
Question 2: Is there an error in the get method logic?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.