文字

范例

Table of Contents

  • curl 基础例子

用户评论:

[#1] shashank at neupane dot com dot np [2013-03-04 09:43:43]

to get the full webpages from head as well as body

<?php
$curl 
curl_init('http://example.com');
curl_setopt($curlCURLOPT_FAILONERRORtrue);
curl_setopt($curlCURLOPT_FOLLOWLOCATIONtrue);
curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
curl_setopt($curlCURLOPT_SSL_VERIFYHOSTfalse);
curl_setopt($curlCURLOPT_SSL_VERIFYPEERfalse);   
$result curl_exec($curl);
echo 
$result;
?>

[#2] William at crandell dot ws [2010-02-19 15:11:25]

I have found that cURL is pretty easy to use overall.  Though I have noticed that many have tried and struggled with using cURL and getting a ASP.NET or .aspx form/ page to give back a response.  I am included in the people who have struggled.
To note it seems to be something with the way the form authenticates.

From what I have seen and gathered ASP.NET has a "postback" function using AJAX, __EVENTTARGET, __EVENTARGUMENT and viewstate.  That is where you need to start and make use of the post option with cURL.

Though I do not have an example to offer, I understand that you may need to do more than 1 curl session to complete everything.  One to post and one to get.

[#3] vojta at draxl dot eu [2009-08-16 14:34:43]

The previous solution is useful for passing through The Google Docs form and also handle the filled data at own server.

You can add the missing feature of confirmation mail with own script and more

PHP converts the dots in the variable's names to underscore. So dont forget to replace back the _ with . in the names of POST variables before sending to google.

[#4] ac at zwerg dot at [2009-06-26 04:46:22]

If you want to write a sort of php wrapper to include the results of another http(s) request maybe pointing to a totally different site or just different code (mod_perl with HTML::Mason, in my case) into a php based layout, and just pass-thru all GET and POST variables to the sub-request, the following snippet can be used. Note there is no error handling, so this is subject to the underlying application.

<?php
$ch 
curl_init($sub_req_url);
$encoded '';
// include GET as well as POST variables; your needs may vary.
foreach($_GET as $name => $value) {
  
$encoded .= urlencode($name).'='.urlencode($value).'&';
}
foreach(
$_POST as $name => $value) {
  
$encoded .= urlencode($name).'='.urlencode($value).'&';
}
// chop off last ampersand
$encoded substr($encoded0strlen($encoded)-1);
curl_setopt($chCURLOPT_POSTFIELDS,  $encoded);
curl_setopt($chCURLOPT_HEADER0);
curl_setopt($chCURLOPT_POST1);
curl_exec($ch);
curl_close($ch);
?>

[#5] cmnajs at gmail dot com [2009-01-07 15:57:16]

Following code returns the curl output as a string.

<?php
        
// create curl resource
        
$ch curl_init();

        
// set url
        
curl_setopt($chCURLOPT_URL"example.com");

        
//return the transfer as a string
        
curl_setopt($chCURLOPT_RETURNTRANSFER1);

        
// $output contains the output string
        
$output curl_exec($ch);

        
// close curl resource to free up system resources
        
curl_close($ch);      
?>

[#6] randearievilo at gmail dot com [2008-06-16 10:39:21]

This simple code work fine using libcurl versions before 7.18.x.

<?php
        $ch 
curl_init("www.example.com/curl.php?option=test");
        
curl_setopt($chCURLOPT_HEADER0);
        
curl_setopt($chCURLOPT_POST1);
        
curl_setopt($chCURLOPT_RETURNTRANSFER1);
        
$output curl_exec($ch);       
        
curl_close($ch);
        echo 
$output;
?>


If you have some trouble (Ex.: Failed to open/read local data from file/application) using a similar code with libcurl 7.18.x, try to add the option curl_setopt($ch, CURLOPT_POSTFIELDS, "").

I think there was a change in the option CURLOPT_POST. Now, to use this option, it seems to be necessary to set the option CURLOPT_POSTFIELDS.

[#7] jlee8df at gmail dot com [2008-05-30 13:47:31]

Basic cURL file or page download with basic error trapping.

<?php

function cURLcheckBasicFunctions()
{
  if( !
function_exists("curl_init") &&
      !
function_exists("curl_setopt") &&
      !
function_exists("curl_exec") &&
      !
function_exists("curl_close") ) return false;
  else return 
true;
}

/*
 * Returns string status information.
 * Can be changed to int or bool return types.
 */
function cURLdownload($url$file)
{
  if( !
cURLcheckBasicFunctions() ) return "UNAVAILABLE: cURL Basic Functions";
  
$ch curl_init();
  if(
$ch)
  {
    
$fp fopen($file"w");
    if(
$fp)
    {
      if( !
curl_setopt($chCURLOPT_URL$url) )
      {
        
fclose($fp); // to match fopen()
        
curl_close($ch); // to match curl_init()
        
return "FAIL: curl_setopt(CURLOPT_URL)";
      }
      if( !
curl_setopt($chCURLOPT_FILE$fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
      if( !
curl_setopt($chCURLOPT_HEADER0) ) return "FAIL: curl_setopt(CURLOPT_HEADER)";
      if( !
curl_exec($ch) ) return "FAIL: curl_exec()";
      
curl_close($ch);
      
fclose($fp);
      return 
"SUCCESS: $file [$url]";
    }
    else return 
"FAIL: fopen()";
  }
  else return 
"FAIL: curl_init()";
}

// Download from 'example.com' to 'example.txt'
echo cURLdownload("http://www.example.com""example.txt");

?>


- JL????

上一篇: 下一篇: