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 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题