【问题标题】:file_get_contents with header: Send current header or current cookie in magento or commonfile_get_contents with header:以 magento 或 common 发送当前标头或当前 cookie
【发布时间】:2013-10-30 21:29:35
【问题描述】:

我想调用一个 URL 并希望通过使用 file_get_contents 使用 PHP 获取结果(我知道 CURL,但首先我想使用 file_get_contents 尝试它)。在我的情况下,它是对magento shop 系统的请求,它需要先前完成的后端登录。

如果我在浏览器中手动执行 URL,就会出现正确的页面。如果我使用file_get_contents 发送 URL,我也会登录(因为我在请求中添加了 Cookie),但每次我只获得仪表板主站点时,可能会导致重定向。

我尝试模拟相同的 http 请求,因为我的浏览器将其发送出去。我的问题是:是否可以直接将相同的标头数据(Cookie、Session-ID 等)作为参数发送到file_get_contents,而无需手动序列化?

这是一个常见的 PHP 问题,基本脚本是:

$postdata = http_build_query(
    array(
        'var1' => 'some content',
        'var2' => 'doh'
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context  = stream_context_create($opts);

$result = file_get_contents('http://example.com/submit.php', false, $context);

在我的例子中,代码是:

        $postdata = http_build_query(
            array
            (
                'selected_products' => 'some content',
            )
        );

        $opts = array('http' =>
            array
            (
                'method'  => 'POST',
                'header'  => "Content-type: application/x-www-form-urlencoded; charset=UTF-8\r\n".
                             "Cookie: __utma=".Mage::getModel('core/cookie')->get("__utma").";".
                             "__utmz=".Mage::getModel('core/cookie')->get("__utmz").
                             " __utmc=".Mage::getModel('core/cookie')->get("__utmc").';'.
                             "adminhtml=".Mage::getModel('core/cookie')->get("adminhtml")."\r\n".
                             "X-Requested-With: XMLHttpRequest\r\n".
                             "Connection: keep-alive\r\n".
                             "Accept: text/javascript, text/html, application/xml, text/xml, */*\r\n".
                             "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0",
                'content' => $postdata
            )
        );

$context  = stream_context_create($opts);
var_dump(file_get_contents($runStopAndRemoveProducts, false, $context ));

结果应该与我通过手动调用 URL 在浏览器中得到的错误消息相同(“请选择一些产品”作为纯文本),但响应是作为 html 网站的完整仪表板主页。

我正在寻找这样的脚本。我想确保所有参数都是自动设置的,无需手动构建 cookie 字符串和其他参数 :)

file_get_contents('http://example.com/submit.php', false, $_SESSION["Current_Header"]);

编辑:我发现了错误,需要两个特殊的 get-Parameter(isAjax=1form_key = Mage::getSingleton('core/session', array('name' => 'adminhtml'))->getFormKey())。在我的情况下,form_key 会导致错误。但丑陋的 Cookie 字符串已经存在 - 仍在寻找更漂亮的解决方案。

【问题讨论】:

    标签: php magento cookies http-headers file-get-contents


    【解决方案1】:

    在我看来,这看起来就像您正在尝试为您可以更优雅、正确、完整记录的方式编写一些技巧。请查看 Magento API。

    如果您想删除产品(或做其他事情):

    http://www.magentocommerce.com/api/soap/catalog/catalogProduct/catalog_product.delete.html

    您将得到适当的回复,以了解事情是否成功。如果 API 无法完成某些事情,那么您可以根据需要扩展/破解它。

    要开始使用,您需要一个 API 用户/通行证并快速掌握 SOAP。 Magento 文档中的示例就足够了。祝你好运!

    【讨论】:

    • 一般来说你是对的 :-) 在这种情况下,我们使用“M2E Pro”扩展从 ERP 软件创建 eBay/亚马逊列表/产品。所以我们必须调用 M2E-Pro 函数,不幸的是 SOAP 或类似的东西不可用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 2021-02-07
    • 2018-11-14
    相关资源
    最近更新 更多