PHP - 读取文本文件并在POST请求中按顺序使用每个行值
Hello :) I apologize in advance if this is really dumb, but I've been trying to figure it out for a week and now I'm about to explode :D
I have a list of 200 ID numbers in a text file, each are separated by a newline. I want to use PHP to use each line, one by one, as a variable in a POST request. I can use this code to send the first line, by specifying the number of characters to include from the file:
$IDfile = "file.txt";
$fh = fopen($IDfile, 'r');
$ID = fread($fh, 18);
fclose($fh);
Then I can use the $ID variable in the POST. But this is only good for the first line, since there are 18 characters in the line, but there are 199 more lines.
I can also print all the lines in the file with a foreach, but this won't help with the POST requests:
$IDfile = "file.txt";
$lines = file($IDfile);
foreach($lines as $line_num => $line)
{
echo $line;
echo "<br>";
}
So, here is the full script of my POST request, to send a "destroy tweet" command to the Twitter API 1.1 - The $ID variable is the one I want to replace using every line in the text file sequentially:
require_once('TwitterAPIExchange.php');
include('apikeys.php');
$url = "https://api.twitter.com/1.1/statuses/destroy/" .$ID . ".json";
$requestMethod = 'POST';
$postfields = array(
);
$twitter = new TwitterAPIExchange($settings);
$twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
So how do I do it? How can I get each line sequentially from the text file, and then use the single line value as a variable in the POST request, until each has been read?
This is probably way out of my league lol, but I am determined to learn this, and any help or direction would be so very gratefully received :)
Try this, if i am right your must contain the id
field
<?php
require_once( "TwitterAPIExchange.php" );
include( "apikeys.php" );
$url = "https://api.twitter.com/1.1/statuses/destroy/%s.json";
$lines = file( "file.txt" );
$post = array(
"id" => null
);
foreach( $lines as $line ) {
$line = trim( $line );
$post["id"] = $line;
$twitter = new TwitterAPIExchange( $settings );
$response = $twitter->buildOauth( sprintf( $url, $line ), "POST" )
->setPostfields( $post )
->performRequest();
$response = json_decode( $response );
if ( !$response ) {
echo "Empty response";
break;
}
var_dump( $response );
}
?>
Use fopen
to read the file and then inside the loop you can create the URL and send a POST request, like so:
$f = fopen("file.txt", "r");
while(!feof($f)) {
$url = "https://api.twitter.com/1.1/statuses/destroy/" . trim($f) . ".json";
$requestMethod = 'POST';
$postfields = array(
);
$twitter = new TwitterAPIExchange($settings);
$twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
}
Something along these lines should help you solve that issue
$IDfile = "file.txt";
$lines = file($IDfile);
foreach($lines as $line_num => $line)
{
echo sending request for $line;
$url = "https://api.twitter.com/1.1/statuses/destroy/" .$line . ".json";
$requestMethod = 'POST';
$postfields = array(
);
$twitter = new TwitterAPIExchange($settings);
$twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
}