如何使用PHP CURL与框架? [英] How to use PHP CURL with frames?

查看:141
本文介绍了如何使用PHP CURL与框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用php curl为我的学校网络创建一个登录脚本,但我遇到了一个障碍。我有我的脚本登录,但主页是由框架组成。内容没有显示任何框架,说在服务器上找不到网址。每个框架的URL是相对的,所以浏览器在我的服务器上查找文件而不是其他服务器(显然不会找到它们)。我用CURLOPT_REFERER设置了referer字段,但问题仍然存在。有没有一个简单的方法来解决这个相对URL问题?

解决方案

首先,我想说我知道是一个老问题,但我在搜索curl解决方案时遇到过几次,认为这对其他人寻找这个答案会有所帮助。



问题澄清



根据我的理解,你可以看到框架,但是你看不到框架的内容,因为框架的src属性相对。

$ p> <?php
//简单curl函数
function get_page($ url){
$ msg =;
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
$ html = curl_exec($ ch);

if(!curl_errno($ ch)){
$ msg = $ html;
} else {
$ msg ='Curl error:'。 curl_error($ ch);
}
curl_close($ ch);

return $ msg;
}
//所需域名
$ domain =http://www.w3schools.com;
$ page_content = get_page($ domain / tags / tryit.asp?filename = tryhtml_iframe);
//显示获取的页面
var_dump($ page_content);
//导航dom到问题中的iframe
$ xml = new DOMDocument();
@ $ xml-> loadHTML($ page_content);
$ path = new DOMXPath($ xml);
$ forms = $ path-> query(// iframe [@ id ='iframeResult']);
$ relative_uri =;
foreach($ forms as $ form){
//应该只有一个结果
if(strtolower($ form-> getAttribute('id'))=== strtolower iframeResult)){
$ relative_uri = $ form-> getAttribute('src');
}
}
//显示提取的iframe
$ page_content = get_page($ domain / tags / $ relative_uri);
var_dump($ page_content);
?>

示例说明



我选择www.w3schools.com,因为我猜他们的例子在一段时间内不会改变,并且可能比一些随机选择的网站有更好的正常运行时间。



我还想说明w3schools网站上的正确示例框架本身是一个相对框架,而不是它们显示的实际示例框架!通过在元素检查器中搜索 iframeResult 来寻找自己。



运行示例 / p>

要导航的页面



http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe



导航页面的php卷曲示例



http://ipatenco.com/example/curl-iframe.php


I've been working with php curl to create a login script for my school's network, but I've hit a snag. I got my script to login, but the home page is made up of frames. The content isn't showing up for either frame, saying the url was not found on the server. The urls for each of the frames are relative, so the browser is looking on my server for the files instead of the other server (and obviously won't find them). I set the referer field with CURLOPT_REFERER, but the problem persists. Is there a simple way to get around this relative url problem?

解决方案

First off, I would like to state that I know this is an old question, but I have come across it a few times when searching for curl solutions and think it would be helpful for other people looking for this answer.

Question Clarification

From what I understand, you can see the frame, but you cannot see the contents of the frame because the src attribute of the frame is relative. You also wish to follow the frame through php curl.

Example Code

<?php
// Simple curl function
function get_page($url){
    $msg = "";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $html = curl_exec($ch);

    if (!curl_errno($ch)) {
        $msg = $html;
    } else {
        $msg = 'Curl error: ' . curl_error($ch);
    }
    curl_close($ch);

    return $msg;
}
// Desired domain
$domain = "http://www.w3schools.com";
$page_content = get_page("$domain/tags/tryit.asp?filename=tryhtml_iframe");
// Display fetched page
var_dump($page_content);
// Navigate dom to iframe in question
$xml = new DOMDocument();
@$xml->loadHTML($page_content);
$path = new DOMXPath($xml);
$forms = $path->query("//iframe[@id='iframeResult']");
$relative_uri = "";
foreach ($forms as $form) {
    // Should be only 1 result
    if (strtolower($form->getAttribute('id')) === strtolower("iframeResult")) {
        $relative_uri = $form->getAttribute('src');
    }
}
// Display fetched iframe
$page_content = get_page("$domain/tags/$relative_uri");
var_dump($page_content);
?>

Notes on Example

I choose www.w3schools.com because I am guessing that their example won't change in a while and will probably have better up-time than some randomly selected website.

I would also like to state that the right example frame itself on the w3schools website is a relative frame, not the actual example frame they show! Look for yourself by searching for iframeResult in an element inspector.

Running Example

Page to be navigated

http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe

Example of php curl navigating the page

http://ipatenco.com/example/curl-iframe.php

这篇关于如何使用PHP CURL与框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆