簡體   English   中英

cURL無法確認函數參數

[英]cURL fails to acknowledge function argument

我正在使用php.net手冊上流行的注釋中的cURL函數,盡管該函數的參數(用於指定URL)未被識別或初始化。 現在,當我啟用CURLOPT_URL並在數組中指定URL時,它將顯示網頁,並且數組返回適當的信息,但是我不希望顯示該網頁,只需在數組中返回信息,但是curl_exec是必需的總而言之,這總是顯示網頁。

我究竟做錯了什么?

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

嘗試使用

CURLOPT_RETURNTRANSFER => false, 

我所知道的,如果將returntransfer設置為TRUE,則該網頁顯示為瀏覽器。 所有過濾器和其他編碼都在returntranfer之后,因此您無法查看它們

不用擔心,如果將其設置為false,仍然curl將獲取頁面並執行您將要編寫的代碼。

希望對您有幫助。

試試這個,看看它是否滿足您的要求。 我實際上並沒有做太大改變,只是重構了代碼以使其更易於閱讀,並添加了一些更具描述性/可讀性的注釋來解釋一切。 在我更改了某些內容的地方,有一條評論解釋了我更改了原因。

<?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