douxie0824 2016-08-20 10:28
浏览 45
已采纳

从其他网站获取输入字段的值html代码

I'm trying to get the value of a hidden input field from twitters follow page. I'm using file_get_contents on the url and then try to get the value of the input with the preg_match_all function but something in my code isn't working because I just get back an empty array. I would really appreciate it if someone would look over my code and help me get it to work.

HTML Code of the input field I want to get the value from (in this example twitter)

<input type="hidden" name="screen_name" value="twitter">

My code snippet I don't get to work

$html = file_get_contents($url);
preg_match_all("/<input type=\"hidden\" name=\"screen_name\" value=\"(.*?)\">/", $html, $screen_name);
echo "<pre>", print_r($screen_name, true), "</pre>";

This code should output the value of the input field in this example just twitter within an array.

Edit: My code snippet works find I just haven't noticed that Twitter only shows this hidden input field if your logged in on twitter and of course if you use file_get_contents your webserver will not be logged in to twitter and can't get the HTML code what you get if you are logged in. Thanks to vigikaran for pointing this out to me and to gilbert for improving the regex in my code snippet.

  • 写回答

3条回答 默认 最新

  • duanjiu1950 2016-08-20 10:56
    关注

    Thanks to vigikaran for pointing this out to me my code is fine and works but the HTML code I got from twitter doesn't contain the hidden input field I was looking for because It only shows this input field if your logged in and of course if you use file_get_contents your webserver will not be logged in. Thanks to everyone for the help and to gilbert for improving the regex from my code snippet.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计