📄 curl.c
字号:
error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDS, post); error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_PP(zvalue)); } break; case CURLOPT_HTTPHEADER: case CURLOPT_QUOTE: case CURLOPT_POSTQUOTE: { zval **current; HashTable *ph; struct curl_slist *slist = NULL; ph = HASH_OF(*zvalue); if (!ph) { php_error(E_WARNING, "%s(): You must pass either an object or an array with the CURLOPT_HTTPHEADER, " "CURLOPT_QUOTE and CURLOPT_POSTQUOTE arguments", get_active_function_name(TSRMLS_C)); RETURN_FALSE; } for (zend_hash_internal_pointer_reset(ph); zend_hash_get_current_data(ph, (void **) ¤t) == SUCCESS; zend_hash_move_forward(ph)) { char *indiv = NULL; SEPARATE_ZVAL(current); convert_to_string_ex(current); indiv = estrndup(Z_STRVAL_PP(current), Z_STRLEN_PP(current) + 1); slist = curl_slist_append(slist, indiv); if (! slist) { efree(indiv); php_error(E_WARNING, "%s(): Couldn't build curl_slist", get_active_function_name(TSRMLS_C)); RETURN_FALSE; } zend_llist_add_element(&ch->to_free.str, &indiv); } zend_llist_add_element(&ch->to_free.slist, &slist); error = curl_easy_setopt(ch->cp, option, slist); break; } /* the following options deal with files, therefor safe_mode & open_basedir checks * are required. */ case CURLOPT_COOKIEJAR: case CURLOPT_SSLCERT: case CURLOPT_RANDOM_FILE: case CURLOPT_COOKIEFILE: { char *copystr = NULL; convert_to_string_ex(zvalue); if (php_check_open_basedir(Z_STRVAL_PP(zvalue) TSRMLS_CC) || (PG(safe_mode) && !php_checkuid(Z_STRVAL_PP(zvalue), "rb+", CHECKUID_CHECK_MODE_PARAM))) { RETURN_FALSE; } copystr = estrndup(Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue)); error = curl_easy_setopt(ch->cp, option, copystr); zend_llist_add_element(&ch->to_free.str, ©str); break; } } SAVE_CURL_ERROR(ch, error); if (error != CURLE_OK) { RETURN_FALSE; } else { RETURN_TRUE; }}/* }}} *//* {{{ cleanup_handle(ch) Cleanup an execution phase */static void cleanup_handle(php_curl *ch){ if (ch->handlers->write->buf.len > 0) { smart_str_free(&ch->handlers->write->buf); ch->handlers->write->buf.len = 0; } memset(ch->err.str, 0, CURL_ERROR_SIZE + 1); ch->err.no = 0;}/* }}} *//* {{{ proto bool curl_exec(resource ch) Perform a CURL session */PHP_FUNCTION(curl_exec){ zval **zid; php_curl *ch; CURLcode error; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &zid) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); cleanup_handle(ch); error = curl_easy_perform(ch->cp); SAVE_CURL_ERROR(ch, error); /* CURLE_PARTIAL_FILE is returned by HEAD requests */ if (error != CURLE_OK && error != CURLE_PARTIAL_FILE) { if (ch->handlers->write->buf.len > 0) { smart_str_free(&ch->handlers->write->buf); ch->handlers->write->buf.len = 0; } RETURN_FALSE; } ch->uses++; if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.len > 0) { --ch->uses; if (ch->handlers->write->type != PHP_CURL_BINARY) { smart_str_0(&ch->handlers->write->buf); } RETURN_STRINGL(ch->handlers->write->buf.c, ch->handlers->write->buf.len, 1); } --ch->uses; if (ch->handlers->write->method == PHP_CURL_RETURN) { RETURN_EMPTY_STRING(); } RETURN_TRUE;}/* }}} *//* {{{ proto mixed curl_getinfo(resource ch, int opt) Get information regarding a specific transfer */PHP_FUNCTION(curl_getinfo){ zval **zid, **zoption; php_curl *ch; int option, argc = ZEND_NUM_ARGS(); if (argc < 1 || argc > 2 || zend_get_parameters_ex(argc, &zid, &zoption) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); if (argc < 2) { char *s_code; long l_code; double d_code; array_init(return_value); if (curl_easy_getinfo(ch->cp, CURLINFO_EFFECTIVE_URL, &s_code) == CURLE_OK) { CAAS("url", s_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_TYPE, &s_code) == CURLE_OK) { if (s_code != NULL) { CAAS("content_type", s_code); } } if (curl_easy_getinfo(ch->cp, CURLINFO_HTTP_CODE, &l_code) == CURLE_OK) { CAAL("http_code", l_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_HEADER_SIZE, &l_code) == CURLE_OK) { CAAL("header_size", l_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_REQUEST_SIZE, &l_code) == CURLE_OK) { CAAL("request_size", l_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_FILETIME, &l_code) == CURLE_OK) { CAAL("filetime", l_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_SSL_VERIFYRESULT, &l_code) == CURLE_OK) { CAAL("ssl_verify_result", l_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_COUNT, &l_code) == CURLE_OK) { CAAL("redirect_count", l_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_TOTAL_TIME, &d_code) == CURLE_OK) { CAAD("total_time", d_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_NAMELOOKUP_TIME, &d_code) == CURLE_OK) { CAAD("namelookup_time", d_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_CONNECT_TIME, &d_code) == CURLE_OK) { CAAD("connect_time", d_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_PRETRANSFER_TIME, &d_code) == CURLE_OK) { CAAD("pretransfer_time", d_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_UPLOAD, &d_code) == CURLE_OK) { CAAD("size_upload", d_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_DOWNLOAD, &d_code) == CURLE_OK) { CAAD("size_download", d_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_DOWNLOAD, &d_code) == CURLE_OK) { CAAD("speed_download", d_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_UPLOAD, &d_code) == CURLE_OK) { CAAD("speed_upload", d_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d_code) == CURLE_OK) { CAAD("download_content_length", d_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_UPLOAD, &d_code) == CURLE_OK) { CAAD("upload_content_length", d_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_STARTTRANSFER_TIME, &d_code) == CURLE_OK) { CAAD("starttransfer_time", d_code); } if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_TIME, &d_code) == CURLE_OK) { CAAD("redirect_time", d_code); } } else { option = Z_LVAL_PP(zoption); switch (option) { case CURLINFO_EFFECTIVE_URL: case CURLINFO_CONTENT_TYPE: { char *s_code = NULL; if (curl_easy_getinfo(ch->cp, option, &s_code) == CURLE_OK && s_code) { RETURN_STRING(s_code, 1); } else { RETURN_FALSE; } break; } case CURLINFO_HTTP_CODE: case CURLINFO_HEADER_SIZE: case CURLINFO_REQUEST_SIZE: case CURLINFO_FILETIME: case CURLINFO_SSL_VERIFYRESULT: case CURLINFO_REDIRECT_COUNT: { long code = 0; if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) { RETURN_LONG(code); } else { RETURN_FALSE; } break; } case CURLINFO_TOTAL_TIME: case CURLINFO_NAMELOOKUP_TIME: case CURLINFO_CONNECT_TIME: case CURLINFO_PRETRANSFER_TIME: case CURLINFO_SIZE_UPLOAD: case CURLINFO_SIZE_DOWNLOAD: case CURLINFO_SPEED_DOWNLOAD: case CURLINFO_SPEED_UPLOAD: case CURLINFO_CONTENT_LENGTH_DOWNLOAD: case CURLINFO_CONTENT_LENGTH_UPLOAD: case CURLINFO_STARTTRANSFER_TIME: case CURLINFO_REDIRECT_TIME: { double code = 0.0; if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) { RETURN_DOUBLE(code); } else { RETURN_FALSE; } break; } } } }/* }}} *//* {{{ proto string curl_error(resource ch) Return a string contain the last error for the current session */PHP_FUNCTION(curl_error){ zval **zid; php_curl *ch; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &zid) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); ch->err.str[CURL_ERROR_SIZE] = 0; RETURN_STRING(ch->err.str, 1);}/* }}} *//* {{{ proto int curl_errno(resource ch) Return an integer containing the last error number */PHP_FUNCTION(curl_errno){ zval **zid; php_curl *ch; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &zid) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); RETURN_LONG(ch->err.no);}/* }}} *//* {{{ proto void curl_close(resource ch) Close a CURL session */PHP_FUNCTION(curl_close){ zval **zid; php_curl *ch; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &zid) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); if (ch->in_callback) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to close CURL handle from a callback"); return; } if (ch->uses) { ch->uses--; } else { zend_list_delete(Z_LVAL_PP(zid)); }}/* }}} *//* {{{ _php_curl_close() List destructor for curl handles */static void _php_curl_close(zend_rsrc_list_entry *rsrc TSRMLS_DC){ php_curl *ch = (php_curl *) rsrc->ptr; curl_easy_cleanup(ch->cp); zend_llist_clean(&ch->to_free.str); zend_llist_clean(&ch->to_free.slist); zend_llist_clean(&ch->to_free.post); if (ch->handlers->write->buf.len > 0) { smart_str_free(&ch->handlers->write->buf); ch->handlers->write->buf.len = 0; } if (ch->handlers->write->func) { FREE_ZVAL(ch->handlers->write->func); ch->handlers->read->func = NULL; } if (ch->handlers->read->func) { zval_ptr_dtor(&ch->handlers->read->func); ch->handlers->read->func = NULL; } if (ch->handlers->write_header->func) { FREE_ZVAL(ch->handlers->write_header->func); ch->handlers->write_header->func = NULL; } if (ch->handlers->passwd) { FREE_ZVAL(ch->handlers->passwd); ch->handlers->passwd = NULL; } efree(ch->handlers->write); efree(ch->handlers->write_header); efree(ch->handlers->read); efree(ch->handlers); efree(ch);} /* }}} */#endif /* HAVE_CURL *//* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: fdm=marker * vim: noet sw=4 ts=4 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -