简体   繁体   English

cURL无法确认函数参数

[英]cURL fails to acknowledge function argument

I am using a cURL function from a popular comment on the php.net manuals , though the argument for the function, which is to specify a URL, isn't being recognized or initiated. 我正在使用php.net手册上流行的注释中的cURL函数,尽管该函数的参数(用于指定URL)未被识别或初始化。 Now when i enable the CURLOPT_URL and specify the URL there in the array, it displays the webpage and the array returns the appropriate information, but I do not want the webpage to be displayed, just return the information in the array, but curl_exec is needed in the sum of it all and that always displays the webpage. 现在,当我启用CURLOPT_URL并在数组中指定URL时,它将显示网页,并且数组返回适当的信息,但是我不希望显示该网页,只需在数组中返回信息,但是curl_exec是必需的总而言之,这总是显示网页。

What am i doing wrong? 我究竟做错了什么?

//defines the function get_web_page
$url = "http://ulrichzukowebtv.comli.com";
    function get_web_page($url)
{
    $options = array(
        //CURLOPT_URL => 'http://ulrichzukowebtv.comli.com',

        CURLOPT_RETURNTRANSFER => true,     // return web page

        CURLOPT_HEADER         => false,    // don't return headers

        CURLOPT_FOLLOWLOCATION => true,     // follow redirects

        CURLOPT_ENCODING       => "",       // handle compressed

        CURLOPT_USERAGENT      => "spider", // who am i

        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();

    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;

} //end defining function


$curl_data = "var1=60&var2=test";

//call the function
$response = get_web_page($url, $curl_data);

print '<pre>';

//Returns an array containing all the CURL flags specified
print_r($response); 

try using 尝试使用

CURLOPT_RETURNTRANSFER => false, 

what i know, if returntransfer is set to TRUE, the web page is displayed in brower. 我所知道的,如果将returntransfer设置为TRUE,则该网页显示为浏览器。 all the filter and other coding comes after the returntranfer, so you are unable to view them 所有过滤器和其他编码都在returntranfer之后,因此您无法查看它们

and dont worry, if it is set to false, still curl will get the page and will do the task whatever you will code.. 不用担心,如果将其设置为false,仍然curl将获取页面并执行您将要编写的代码。

Hoping This will help you.. 希望对您有帮助。

Try this and see if it does what you want. 试试这个,看看它是否满足您的要求。 I haven't actually changed that much, just refactored the code to make it easier to read and added some more descriptive/readable comments to explain what everything does. 我实际上并没有做太大改变,只是重构了代码以使其更易于阅读,并添加了一些更具描述性/可读性的注释来解释一切。 Where I have changed something, there is a comment explaining what I changed an why. 在我更改了某些内容的地方,有一条评论解释了我更改了原因。

<?php

  function get_web_page ($url, $params = NULL) {

    // Add a query string to the URL if one was supplied.
    // Previously your second argument $curl_data was unhandled and ignored.
    if (is_array($params) || is_object($params)) {
      // Build a query string from an associative array or object and append to the URL
      $url .= '?'.http_build_query($params);
    } else if ($params !== NULL) {
      // Append scalar values directly to the URL
      $url .= '?'.$params;
    }

    // Define the options for cURL
    $options = array (
      CURLOPT_URL            => $url,     // URL we will be loading
      CURLOPT_RETURNTRANSFER => true,     // Return data instead of outputting it
      CURLOPT_HEADER         => false,    // Omit headers from the return value
      CURLOPT_FOLLOWLOCATION => true,     // Follow redirects
      CURLOPT_ENCODING       => "",       // Handle compressed
      CURLOPT_USERAGENT      => "spider", // Tell the server who we are
      CURLOPT_AUTOREFERER    => true,     // Set referer on redirect
      CURLOPT_CONNECTTIMEOUT => 120,      // Timeout on connect (2 minutes is a very long time...!)
      CURLOPT_TIMEOUT        => 120,      // Timeout on response (2 minutes is a very long time...!)
      CURLOPT_MAXREDIRS      => 10        // Stop after 10 redirects
    );

    // Initialise cURL and set the options we defined above
    $ch = curl_init();
    curl_setopt_array($ch, $options);

    // Make the request
    $content = curl_exec($ch);

    // Fetch the result info into an array
    $result = array();
    $result['options'] = $options;         // The options used for the request
    $result['content'] = $content;         // The body of the response from the server
    $result['header'] = curl_getinfo($ch); // Information about the request, including some headers
    $result['errno'] = curl_errno($ch);    // The numeric error code of the result
    $result['errmsg'] = curl_error($ch);   // The textual error of the result

    // Close the curl handle
    curl_close($ch);

    // Return the result info
    return $result;

  } // End function definition

  // Define the URL to load
  $url = "http://ulrichzukowebtv.comli.com/"; // You need the trailing forward slash if you are loading the root of the domain

  // Define the GET parameters to pass
  // We'll define this as an array to make if easier to read
  $params = array (
    'var1' => 60,
    'var2' => 'test'
  );

  // Call the function
  $response = get_web_page($url, $params);

  // Show the result - if you don't want the page to be output, remove these three lines
  print '<pre>';
  print_r($response);
  print '<pre>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM