ds34222 2013-08-16 01:45
浏览 46
已采纳

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 :)

  • 写回答

3条回答 默认 最新

  • duanba3707 2013-08-16 02:37
    关注

    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 );
      }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 写uniapp时遇到的问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流