Reputation: 13
For example, I want to curl 8 text files from http://example.com/0.txt to http://example.com/7.txt and echo them out, in a loop. The following is my current codes.
for ($i=0; $i < 6; $i++) {
$curl = curl_init();
$post_url = 'http://example.com/' . i . '.txt';
curl_setopt($curl, CURLOPT_URL, $post_url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
echo '<article>' . $data . '</article>';
}
It seems that traditionally the value of key CURLOPT_URL
is supposed to directly written in the 4th line, but the way doesn't meet my demand. I tried separating the string with two dots and inserting the loop-controlling variable i
into the two strings, to form the third parameter, but it doesn't work.
Then I tried bring the statement out from the line, and set a variable in advance which combines two strings and one number variable and place the variable in the curl_setopt()
function as the third parameter. It doesn't work.
By the time I search "php curl loop" or "curl in loop with parameter" or something else, Google can hardly provide me with helpful information.
So eventually I am seeking help here : (
Thanks in advance if anyone would like to offer solution.
Upvotes: 1
Views: 1058
Reputation: 26441
Not sure it can be because of typo but try fixing this & check,
$post_url = 'http://example.com/' . $i . '.txt'; // You miss '$' here
^
Upvotes: 2