首页 > 解决方案 > 无法在生产网络上卷曲网站,只能在 localhost - php 中工作

问题描述

我在 curl 网站时遇到问题:http: //hdonline.vn/phim-into-the-badlands-3-15477.html

这是真实的代码(它在本地主机中工作):

<?php
    /**
     * Get a web file (HTML, XHTML, XML, image, etc.) from a URL.  Return an
     * array containing the HTTP server response header fields and content.
     */
    function get_web_page( $url )
    {
        $user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';

        $options = array(

            CURLOPT_CUSTOMREQUEST  =>"GET",        //set request type post or get
            CURLOPT_POST           =>false,        //set to GET
            CURLOPT_USERAGENT      => $user_agent, //set user agent
            CURLOPT_COOKIEFILE     =>"cookie.txt", //set cookie file
            CURLOPT_COOKIEJAR      =>"cookie.txt", //set cookie jar
            CURLOPT_RETURNTRANSFER => true,     // return web page
            CURLOPT_HEADER         => false,    // don't return headers
            CURLOPT_FOLLOWLOCATION => true,     // follow redirects
            CURLOPT_ENCODING       => "",       // handle all encodings
            CURLOPT_AUTOREFERER    => true,     // set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
            CURLOPT_TIMEOUT        => 120,      // timeout on response
            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
        );

        $ch      = curl_init( $url );
        curl_setopt_array( $ch, $options );
        $content = curl_exec( $ch );
        $err     = curl_errno( $ch );
        $errmsg  = curl_error( $ch );
        $header  = curl_getinfo( $ch );
        curl_close( $ch );

        $header['errno']   = $err;
        $header['errmsg']  = $errmsg;
        $header['content'] = $content;
        return $header;
    }
    $result = get_web_page('http://hdonline.vn/phim-into-the-badlands-3-15477.html');

    if ( $result['errno'] != 0 )
        echo 'lỗi';

    if ( $result['http_code'] != 200 )
        echo 'không có trang, ko có quyền';

    $page = $result['content'];
    print_r($page);
?>

您可以复制所有代码并创建新页面进行测试。

问题是:我将源上传到我的 VPS,这里是生产站点: https ://sharengay.com/hdonline/curlhdonline.php

它什么都不显示。

在本地主机中:

https://sharengay.com/hdonline

我在第一个要传递的 php 文件中添加了此代码:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");

标签: javascriptphp

解决方案


推荐阅读