Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CURL使用GET方法代替POST_Php_Curl - Fatal编程技术网

Php CURL使用GET方法代替POST

Php CURL使用GET方法代替POST,php,curl,Php,Curl,我有一个数据提交系统,通过curl将值发布到如下url: $URL="http://some_url/"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$URL); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Content-Type:appli

我有一个数据提交系统,通过curl将值发布到如下url:

  $URL="http://some_url/"; 
           $ch = curl_init(); 
           curl_setopt($ch, CURLOPT_URL,$URL); 
       curl_setopt($ch, CURLOPT_POST, 1); 
       curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Content-Type:application/x-www-form-urlencoded;application/xml'));
       curl_setopt($ch, CURLOPT_POSTFIELDS, "First Name=".$firstname."&Last Name=".$lastname."&Daytime Phone=".$dayphone."&Evening Phone=".$evephone."&DeliveryCode=".$deliveryCode."&username=".$username."&password=".$password);
    echo curl_exec ($ch); 
    curl_close ($ch); 

当我检查网络控制台时,它显示执行了GET请求,而不是按照所需的POST。有什么问题。

我不知道为什么你的代码不起作用,但我给你一个能起作用的代码;这可能不是解决办法,但它会帮助你理解旋度

根据需要调整代码

<?php

class CurlTool {

    public static $userAgents = array(
        'FireFox3' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0',
        'GoogleBot' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
        'IE7' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
        'Netscape' => 'Mozilla/4.8 [en] (Windows NT 6.0; U)',
        'Opera' => 'Opera/9.25 (Windows NT 6.0; U; en)'
    );
    public static $options = array(
        CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
        CURLOPT_AUTOREFERER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FRESH_CONNECT => true,
        CURLOPT_COOKIEJAR => "cookies.txt",
        CURLOPT_COOKIEFILE => "cookies.txt",
        CURLOPT_SSL_VERIFYPEER => false,
            //CURLOPT_COOKIESESSION => false,
    );
    private static $proxyServers = array();
    private static $proxyCount = 0;
    private static $currentProxyIndex = 0;
    public static $getinfo;

    public static function addProxyServer($url) {
        self::$proxyServers[] = $url;
        ++self::$proxyCount;
    }

    public function curl_redirect_exec($ch, &$redirects, $curlopt_header = false) {
/*
        curl_setopt($ch, CURLOPT_USERAGENT ,'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)');
        curl_setopt($ch, CURLOPT_AUTOREFERER ,true);
        curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
        curl_setopt($ch, CURLOPT_COOKIEFILE,"cookies.txt");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER ,false);
        */
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo $redirects."     ";
if ($redirects>0) {
    echo "rr: ".$url;
    exit;
}       
$data = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($http_code == 301 || $http_code == 302) {

    list($header) = explode("\r\n\r\n", $data, 2);
    $matches = array();
    //this part has been changes from the original
    preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
    $url = trim(str_replace($matches[1],"",$matches[0]));
    //end changes
    $url_parsed = parse_url($url);

    if (isset($url_parsed)) {


        $ok=curl_setopt($ch, CURLOPT_URL, $url);
        if (!$ok) {
           echo "doesn't work";
           exit;
        }
        print_r($url_parsed);
    exit;
        $redirects++;
        if($redirects>10)die('live is hard');

        return curl_redirect_exec($ch, $redirects,$curlopt_header);
    }
}
if ($curlopt_header)
    return $data;
else {
    list(,$body) = explode("\r\n\r\n", $data, 2);
    return $body;
}
}

    public static function fetchContent($url, $fields = null, $verbose = false) {
        //print '*'.$fields.'*';
        //
        if (($curl = curl_init($url)) == false) {
            throw new Exception("curl_init error for url $url.");
        }
        if (self::$proxyCount > 0) {
            $proxy = self::$proxyServers[self::$currentProxyIndex++ % self::$proxyCount];
            curl_setopt($curl, CURLOPT_PROXY, $proxy);
            if ($verbose === true) {
                echo "Reading $url [Proxy: $proxy] ... ";
            }
        } else if ($verbose === true) {
            echo "Reading $url ... ";
        }
        //$verbose=TRUE;
        //print_r($fields);
//        debug_print_backtrace();
//url-ify the data for the POST
        $fields_string = '';
        if (is_array($fields))
            foreach ($fields as $key => $value) {
                if (empty($key))
                    continue;
                $fields_string .= $key . '=' . urlencode($value) . '&';
                if ($verbose === true) {
                    echo $key . ": " . $value;
                }
            }
        rtrim($fields_string, '&');

        if (count($fields) > 0) {
            curl_setopt($curl, CURLOPT_POST, count($fields));
            curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
        }

        if ($verbose === true) {
            echo "Fields string $fields_string ... ";
        }
//print_r(self::$options);
//echo phpinfo();
        curl_setopt_array($curl, self::$options);

//curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

/*foreach (self::$options as $option => $value) {
           if (!curl_setopt($curl, $option, $value)) {
           //die('cannot set'.$option.':'.$value);
               //return false;
           } 
       }
    */   
        $html = curl_exec($curl);
        $redirects = 0 ;
        //echo $html;
        //exit;
        //$html = self::curl_redirect_exec($curl, &$redirects);
        //die(2);
        self::$getinfo = curl_getinfo($curl);
        if ($html === false) {
            throw new Exception("curl_exec error for url $url " . curl_error($curl));
        }

        curl_close($curl);
        if ($verbose === true) {
            echo "Done.\n";
        }
        $html = preg_replace('#\n+#', ' ', $html);
        $html = preg_replace('#\s+#', ' ', $html);

        return $html;
    }

    public static function downloadFile($url, $fileName, $fields = null, $verbose = false) {
        if (($curl = curl_init($url)) == false) {
            throw new Exception("curl_init error for url $url.");
        }


        if (self::$proxyCount > 0) {
            $proxy = self::$proxyServers[self::$currentProxyIndex++ % self::$proxyCount];
            curl_setopt($curl, CURLOPT_PROXY, $proxy);
            if ($verbose === true) {
                echo "Downloading $url [Proxy: $proxy] ... ";
            }
        } else if ($verbose === true) {
            echo "Downloading $url ... ";
        }

        //url-ify the data for the POST
        $fields_string = '';
        if (is_array($fields))
            foreach ($fields as $key => $value) {
                if (empty($key))
                    continue;
                $fields_string .= $key . '=' . urlencode($value) . '&';
            }
        rtrim($fields_string, '&');

        curl_setopt($curl, CURLOPT_POST, count($fields));
        curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);

        curl_setopt_array($curl, self::$options);

        if (is_file($fileName)) {
            $contents = file_get_contents($fileName, false, null, -1, 3 * 1024);
            $pattern = "__VIEWSTATE";
            if (strpos($contents, $pattern) === false) {
                return $fileName;
            }
        }

//        if (is_file($fileName)) {
//            // make a HEAD request and try to get the file size HEAD
//            // if they differ then redownload the file, otherwise no need
//            curl_setopt($curl, CURLOPT_NOBODY, true);
//            curl_setopt($curl, CURLOPT_HEADER, true);
//            $ret = curl_exec($curl);
//            //echo $fileName;
//            $size = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
//            if ($size == filesize($fileName)) {
//                return $fileName;
//            } else {
//                unlink($fileName);
//                return self::downloadFile($url, $fileName, $fields, $verbose);
//            }
//        }

        //echo getcwd();

        if (substr($fileName, -1) == '/') {
            $targetDir = $fileName;
            $fileName = tempnam(sys_get_temp_dir(), 'c_');
        }
        if (($fp = fopen('../files/'.$fileName, "w")) === false) {
            throw new Exception("fopen error for filename $fileName");
        }



        curl_setopt($curl, CURLOPT_FILE, $fp);

        curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);

        $ret = curl_exec($curl);
        self::$getinfo = curl_getinfo($curl);

        if ($ret === false) {
            fclose($fp);
            unlink('../files/'.$fileName);
            echo curl_error($curl).'<br/>';
            echo curl_errno($curl).'<br/>';
            if(curl_errno($curl)!=6)
            {
                throw new Exception("curl_exec error for url $url.");
            }
        } elseif (isset($targetDir)) {
            $eurl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
            preg_match('#^.*/(.+)$#', $eurl, $match);
            fclose($fp);
            rename($fileName, "$targetDir{$match[1]}");
            $fileName = "$targetDir{$match[1]}";
        } else {
            fclose($fp);
        }

        curl_close($curl);
        if ($verbose === true) {
            echo "Done.\n";
        }
        return $fileName;
    }

}

为我之前错误的开始回答道歉,但我测试了你的代码,当我请求一个执行打印的页面时,我确实得到了[REQUEST\u METHOD]=>POST($\u SERVER);您是否测试过从同一web服务器获取页面?(即避免任何潜在的外部代理)