SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Some basic questions about cURL and PHP

    Having some understanding problems with curl and some basic php coding problems implementing it.

    PHP.net says curl_init() "Initializes a new session and return a cURL handle "

    What is a new session in this context, and what is a handle? I think of cookies when I think of sessions. I var_dump'd curl_init and just got something like "resource(curl)".

    Why is the standard default variable to hold a curl_init() called $ch? I see it in most curl examples, but I don't know what it stands for. I thought it might be short for curl handle - but I don't know what curl handles are.


    Can someone tell why the in_array function fails to see that '5' is in the array?
    PHP Code:
        $errCodes=array(
            
    => 'CURLE_UNSUPPORTED_PROTOCOL',
            
    => 'CURLE_FAILED_INIT',
            
    => 'CURLE_URL_MALFORMAT',
            
    => 'CURLE_URL_MALFORMAT_USER',
            
    => 'CURLE_COULDNT_RESOLVE_PROXY',
            
    => 'CURLE_COULDNT_RESOLVE_HOST',
            
    => 'CURLE_COULDNT_CONNECT',
            
    => 'CURLE_FTP_WEIRD_SERVER_REPLY',
            
    => 'CURLE_REMOTE_ACCESS_DENIED',
            
    11 => 'CURLE_FTP_WEIRD_PASS_REPLY',
            
    13 => 'CURLE_FTP_WEIRD_PASV_REPLY',
            
    14 =>'CURLE_FTP_WEIRD_227_FORMAT',
            
    15 => 'CURLE_FTP_CANT_GET_HOST',
            
    17 => 'CURLE_FTP_COULDNT_SET_TYPE',
            
    18 => 'CURLE_PARTIAL_FILE',
            
    19 => 'CURLE_FTP_COULDNT_RETR_FILE',
            
    21 => 'CURLE_QUOTE_ERROR',
            
    22 => 'CURLE_HTTP_RETURNED_ERROR',
            
    23 => 'CURLE_WRITE_ERROR',
            
    25 => 'CURLE_UPLOAD_FAILED',
            
    26 => 'CURLE_READ_ERROR',
            
    27 => 'CURLE_OUT_OF_MEMORY',
            
    28 => 'CURLE_OPERATION_TIMEDOUT',
            
    30 => 'CURLE_FTP_PORT_FAILED',
            
    31 => 'CURLE_FTP_COULDNT_USE_REST',
            
    33 => 'CURLE_RANGE_ERROR',
            
    34 => 'CURLE_HTTP_POST_ERROR',
            
    35 => 'CURLE_SSL_CONNECT_ERROR',
            
    36 => 'CURLE_BAD_DOWNLOAD_RESUME',
            
    37 => 'CURLE_FILE_COULDNT_READ_FILE',
            
    38 => 'CURLE_LDAP_CANNOT_BIND',
            
    39 => 'CURLE_LDAP_SEARCH_FAILED',
            
    41 => 'CURLE_FUNCTION_NOT_FOUND',
            
    42 => 'CURLE_ABORTED_BY_CALLBACK',
            
    43 => 'CURLE_BAD_FUNCTION_ARGUMENT',
            
    45 => 'CURLE_INTERFACE_FAILED',
            
    47 => 'CURLE_TOO_MANY_REDIRECTS',
            
    48 => 'CURLE_UNKNOWN_TELNET_OPTION',
            
    49 => 'CURLE_TELNET_OPTION_SYNTAX',
            
    51 => 'CURLE_PEER_FAILED_VERIFICATION',
            
    52 => 'CURLE_GOT_NOTHING',
            
    53 => 'CURLE_SSL_ENGINE_NOTFOUND',
            
    54 => 'CURLE_SSL_ENGINE_SETFAILED',
            
    55 => 'CURLE_SEND_ERROR',
            
    56 => 'CURLE_RECV_ERROR',
            
    58 => 'CURLE_SSL_CERTPROBLEM',
            
    59 => 'CURLE_SSL_CIPHER',
            
    60 => 'CURLE_SSL_CACERT',
            
    61 => 'CURLE_BAD_CONTENT_ENCODING',
            
    62 => 'CURLE_LDAP_INVALID_URL',
            
    63 => 'CURLE_FILESIZE_EXCEEDED',
            
    64 => 'CURLE_USE_SSL_FAILED',
            
    65 => 'CURLE_SEND_FAIL_REWIND',
            
    66 => 'CURLE_SSL_ENGINE_INITFAILED',
            
    67 => 'CURLE_LOGIN_DENIED',
            
    68 => 'CURLE_TFTP_NOTFOUND',
            
    69 => 'CURLE_TFTP_PERM',
            
    70 => 'CURLE_REMOTE_DISK_FULL',
            
    71 => 'CURLE_TFTP_ILLEGAL',
            
    72 => 'CURLE_TFTP_UNKNOWNID',
            
    73 => 'CURLE_REMOTE_FILE_EXISTS',
            
    74 => 'CURLE_TFTP_NOSUCHUSER',
            
    75 => 'CURLE_CONV_FAILED',
            
    76 => 'CURLE_CONV_REQD',
            
    77 => 'CURLE_SSL_CACERT_BADFILE',
            
    78 => 'CURLE_REMOTE_FILE_NOT_FOUND',
            
    79 => 'CURLE_SSH',
            
    80 => 'CURLE_SSL_SHUTDOWN_FAILED',
            
    81 => 'CURLE_AGAIN',
            
    82 => 'CURLE_SSL_CRL_BADFILE',
            
    83 => 'CURLE_SSL_ISSUER_ERROR',
            
    84 => 'CURLE_FTP_PRET_FAILED',
            
    84 => 'CURLE_FTP_PRET_FAILED',
            
    85 => 'CURLE_RTSP_CSEQ_ERROR',
            
    86 => 'CURLE_RTSP_SESSION_ERROR',
            
    87 => 'CURLE_FTP_BAD_FILE_LIST',
            
    88 => 'CURLE_CHUNK_FAILED');

        
    $curlErrno 20;
    if (
    in_array($curlErrno$errCodes))
        {
            echo 
    $errCodes[$curlErrno];
            
        } 

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another pretty basic question. I have saved a result from curl in $html, but when I echo $html, it is in html that can be read by the browser. How can I echo it as just plain text, so <b> is just echoed as <b> and, doesn't make something bold?

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    I think session in this context is meant more like execution session, if that makes any sense. Also curl supports a lot of protocols, which might and might not support sessions and cookies.
    $ch stands for curl handle, which basically is a pointer to resource.
    For checking your array, you should use array_key_exists instead, because 5 is a key not a value.
    As dor second question, you might want to look in direction of php htmlspecialhars function to output readable html.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks ronalds. It does make more sense and the rest of what you said also helps greatly. I messed up and had 5 in the post above, but meant to edit it to be 20, as specified in the code. (I was trying different numbers to see if it was just a special case.)

    I am still a little unclear on what a resource is though, for example, compared to an object.

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    A resource is "a special variable, holding a reference to an external resource" (Source: PHP Manual). The types of Resource are explicitly defined (See the Appendix), and these resources may be referenced by the get_resource_type function. Certain functions will depend on receiving a Resource of a specific Type (mysql_query will expect a "mysql link" type resource, for example). If you dont define a link resource, PHP can automatically find the last-opened 'mysql link' resource, and use that. It couldnt do that if it were just looking for any Object.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •