首页 文章

什么是PHP中的cURL?

提问于
浏览
197

在PHP中,我在许多PHP项目中看到了单词cURL . 它是什么?它是如何工作的?

参考链接:(What is cURL in PHP?

9 回答

  • 215

    cURL是一个允许您在PHP中发出HTTP请求的库 . 您需要了解的所有内容(以及大多数其他扩展程序)都可以在_1582714中找到 .

    为了使用PHP的cURL函数,您需要安装»libcurl包 . PHP要求您使用libcurl 7.0.2-beta或更高版本 . 在PHP 4.2.3中,您将需要libcurl版本7.9.0或更高版本 . 从PHP 4.3.0开始,您需要一个7.9.8或更高版本的libcurl版本 . PHP 5.0.0需要libcurl版本7.10.5或更高版本 .

    您也可以在没有cURL的情况下发出HTTP请求,但需要在 php.ini 文件中启用 allow_url_fopen .

    // Make a HTTP GET request and print it (requires allow_url_fopen to be enabled)
    print file_get_contents('http://www.example.com/');
    
  • 12

    cURL是一种可以从代码中获取URL以从中获取html响应的方法 . cURL表示客户端URL,允许您与其他URL连接并在代码中使用其响应 .

  • 1

    PHP中的CURL:

    Summary:

    PHP中的 curl_exec 命令是从控制台使用 curl 的桥梁 . curl_exec可以轻松快速轻松地执行GET / POST请求,接收来自其他服务器(如JSON)和下载文件的响应 .

    Warning, Danger:

    curl 如果使用不当是邪恶和危险的,因为它只是从互联网上获取数据 . 有人可以介入你的curl和其他服务器并在你的响应中注入一个 rm -rf / 然后为什么我会掉到一个控制台并且 ls -l 不信任任何从curl回来的东西是安全的,即使你正在和你说话自己的服务器你可能会撤回恶意软件以减轻他们财富的愚蠢 .

    示例:

    这些是在Ubuntu 12.10上完成的

    • Basic curl from the commandline:
    el@apollo:/home/el$ curl http://i.imgur.com/4rBHtSm.gif > mycat.gif
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100  492k  100  492k    0     0  1077k      0 --:--:-- --:--:-- --:--:-- 1240k
    

    然后你可以在firefox中打开你的gif:

    firefox mycat.gif
    

    光荣的猫进化弓形虫使妇女养猫,男人同样保持妇女周围 .

    • cURL example get request to hit google.com, echo to the commandline:

    这是通过phpsh终端完成的:

    php> $ch = curl_init();
    
    php> curl_setopt($ch, CURLOPT_URL, 'http://www.google.com');
    
    php> curl_exec($ch);
    

    其中打印并将一堆浓缩的html和javascript(从谷歌)转储到控制台 .

    • cURL example put the response text into a variable:

    这是通过phpsh终端完成的:

    php> $ch = curl_init();
    
    php> curl_setopt($ch, CURLOPT_URL, 'http://i.imgur.com/wtQ6yZR.gif');
    
    php> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    php> $contents = curl_exec($ch);
    
    php> echo $contents;
    

    变量现在包含二进制文件,它是猫的动画GIF,可能性是无限的 .

    • Do a curl from within a PHP file:

    将此代码放在名为myphp.php的文件中:

    <?php
      $curl_handle=curl_init();
      curl_setopt($curl_handle,CURLOPT_URL,'http://www.google.com');
      curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
      curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
      $buffer = curl_exec($curl_handle);
      curl_close($curl_handle);
      if (empty($buffer)){
          print "Nothing returned from url.<p>";
      }
      else{
          print $buffer;
      }
    ?>
    

    然后通过命令行运行它:

    php < myphp.php
    

    你运行myphp.php并通过php解释器执行这些命令,并将大量凌乱的html和javascript转储到屏幕上 .

    您可以使用curl执行 GETPOST 请求,您只需指定此处定义的参数:http://curl.haxx.se/docs/httpscripting.html#POST

    Reminder of danger:

    小心倾倒卷曲输出,如果任何一个被解释和执行,你的盒子是拥有的,你的信用卡信息将出售给第三方,你将得到一个神秘的900美元从阿拉巴马州单人地板公司收取的费用是一个前海外信用卡诈骗犯罪集团 .

  • 18

    cURL是一种可以从代码中获取URL以从中获取HTML响应的方法 . 它用于PHP语言的命令行cURL .

    <?php
    // Step 1
    $cSession = curl_init(); 
    // Step 2
    curl_setopt($cSession,CURLOPT_URL,"http://www.google.com/search?q=curl");
    curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($cSession,CURLOPT_HEADER, false); 
    // Step 3
    $result=curl_exec($cSession);
    // Step 4
    curl_close($cSession);
    // Step 5
    echo $result;
    ?>
    

    第1步:使用 curl_init() 初始化卷曲会话 .

    第2步:为 CURLOPT_URL 设置选项 . 此值是我们向其发送请求的URL . 使用参数 q= 附加搜索词 curl . 设置 CURLOPT_RETURNTRANSFER 的选项 . True会告诉curl返回字符串而不是打印出来 . 设置 CURLOPT_HEADER 的选项,false将告诉curl忽略返回值中的 Headers .

    第3步:使用 curl_exec() 执行curl会话 .

    第4步:关闭我们创建的卷曲会话 .

    第5步:输出返回字符串 .

    public function curlCall($apiurl, $auth, $rflag)
    {
        $ch = curl_init($apiurl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
        if($auth == 'auth') { 
            curl_setopt($ch, CURLOPT_USERPWD, "passw:passw");
        } else {
            curl_setopt($ch, CURLOPT_USERPWD, "ss:ss1");
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $dt = curl_exec($ch);        
        curl_close($ch);
        if($rflag != 1) {
            $dt = json_decode($dt,true);        
        }
        return $dt;
    }
    

    这也用于身份验证 . 我们还可以设置用于身份验证的用户名和密码 .

    有关更多功能,请参阅用户手册或以下教程:

    http://php.net/manual/en/ref.curl.php
    http://www.startutorial.com/articles/view/php-curl

  • 142

    PHP的cURL扩展旨在允许您在PHP脚本中使用各种Web资源 .

  • 6

    首先让我们理解curl,libcurl和PHP / cURL的概念 .

    • curl :用于使用URL语法获取或发送文件的命令行工具 .

    • libcurl :由Daniel Stenberg创建的库,允许您使用许多不同类型的协议连接和通信到许多不同类型的服务器 . libcurl目前支持http,https,ftp,gopher,telnet,dict,file和ldap协议 . libcurl还支持HTTPS证书,HTTP POST,HTTPPUT,FTP上传(这也可以通过PHP的ftp扩展),基于HTTP表单的上传,代理,cookie和用户密码认证 .

    • PHP/cURL :PHP模块使PHP程序可以使用libcurl .

    如何使用它:

    step1 :使用curl_init()初始化卷曲会话 .

    step2 :设置CURLOPT_URL的选项 . 这个值是我们发送请求的URL . 使用参数"q=" .Set选项CURLOPT_RETURNTRANSFER找到一个搜索词"curl",true将告诉curl返回字符串而不是将其打印出来 . 为CURLOPT_HEADER设置选项,false将告诉curl忽略返回值中的 Headers .

    step3 :使用curl_exec()执行curl会话 .

    step4 :关闭我们创建的卷曲会话 .

    step5 :输出返回字符串 .

    Make DEMO

    您需要创建两个PHP文件并将它们放入Web服务器可以从中提供PHP文件的文件夹中 . 在我的情况下,我将它们放入/ var / www /中以简化 .

    1. helloservice.php2. demo.php

    helloservice.php非常简单,基本上只是回传它得到的任何数据:

    <?php
      // Here is the data we will be sending to the service
      $some_data = array(
        'message' => 'Hello World', 
        'name' => 'Anand'
      );  
    
      $curl = curl_init();
      // You can also set the URL you want to communicate with by doing this:
      // $curl = curl_init('http://localhost/echoservice');
    
      // We POST the data
      curl_setopt($curl, CURLOPT_POST, 1);
      // Set the url path we want to call
      curl_setopt($curl, CURLOPT_URL, 'http://localhost/demo.php');  
      // Make it so the data coming back is put into a string
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      // Insert the data
      curl_setopt($curl, CURLOPT_POSTFIELDS, $some_data);
    
      // You can also bunch the above commands into an array if you choose using: curl_setopt_array
    
      // Send the request
      $result = curl_exec($curl);
    
      // Get some cURL session information back
      $info = curl_getinfo($curl);  
      echo 'content type: ' . $info['content_type'] . '
    '; echo 'http code: ' . $info['http_code'] . '
    '; // Free up the resources $curl is using curl_close($curl); echo $result; ?>

    2.demo.php 页面,你可以看到结果:

    <?php 
       print_r($_POST);
       //content type: text/html; charset=UTF-8
       //http code: 200
       //Array ( [message] => Hello World [name] => Anand )
    ?>
    
  • 10

    PHP中的cURL是使用php语言的命令行cURL的桥梁

  • 57

    cURL

    • cURL是一种可以从代码中获取URL以从中获取HTML响应的方法 .

    • 它用于PHP语言的命令行cURL .

    • cURL是一个允许您在PHP中发出HTTP请求的库 .

    PHP支持libcurl,这是一个由Daniel Stenberg创建的库,它允许您使用许多不同类型的协议连接和通信许多不同类型的服务器 . libcurl目前支持http,https,ftp,gopher,telnet,dict,file和ldap协议 . libcurl还支持HTTPS证书,HTTP POST,HTTP PUT,FTP上传(这也可以使用PHP的ftp扩展),基于HTTP表单的上传,代理,cookie和用户密码验证 .

    一旦使用cURL支持编译PHP,就可以开始使用cURL函数了 . cURL函数背后的基本思想是使用curl_init()初始化cURL会话,然后你可以通过curl_setopt()设置所有传输选项,然后你可以用curl_exec()执行会话然后你使用curl_close()完成会话 .

    示例代码

    // error reporting
    error_reporting(E_ALL);
    ini_set("display_errors", 1);
    
    //setting url
    $url = 'http://example.com/api';
    
    //data
    $data = array("message" => "Hello World!!!");
    
    try {
        $ch = curl_init($url);
        $data_string = json_encode($data);
    
        if (FALSE === $ch)
            throw new Exception('failed to initialize');
    
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
            curl_setopt($ch, CURLOPT_TIMEOUT, 5);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    
            $output = curl_exec($ch);
    
        if (FALSE === $output)
            throw new Exception(curl_error($ch), curl_errno($ch));
    
        // ...process $output now
    } catch(Exception $e) {
    
        trigger_error(sprintf(
            'Curl failed with error #%d: %s',
            $e->getCode(), $e->getMessage()),
            E_USER_ERROR);
    }
    

    如需了解更多信息,请查看 -

  • 12

    Curl只不过是PHP的扩展,它继承了主要为Linux / Unix命令行工具编写的普通curl命令和库的行为

    What is Curl? cURL代表客户端URL . cURL用于将数据发送到任何URL . 有关卷曲的详细信息,请访问CURL Website

    PHP中的cURL现在PHP中引入了相同的概念,通过不同的协议(例如HTTP或FTP)将数据发送到任何可访问的URL . 有关详细信息,请参阅PHP Curl Tutorial

相关问题