제목이 혼란 스럽기 때문에 명확하게 설명 할 코드가 있습니다. 덤프 여기 var_dump
기능을 사용하여 배열 내가 분명히 상수 CURLOPT_URL
의 값을PHP 배열의 키를 공통 이름의 상수 값으로 대체 하시겠습니까?
array (size=5)
10002 => string 'http://site2sms.com/userregistration_next.asp' (length=45)
10016 => string 'http://site2sms.com/UserRegistration_Next.asp' (length=45)
10018 => string 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31' (length=108)
47 => int 1
10015 => string 'action=UserCreate&txtFullName=fdsf&genderCombo=Male&birth_day=2&birth_month=12&birth_year=2013&txtEmail=fdsf%40dssad&ProfessCombo=1&StateCombo=Delhi&txtMobileNum=4234&cityCombo=223&Submit=Register' (length=196)
을 가지고 무엇을 할 때
나는 PHP 스크립트$options=array(
CURLOPT_URL => 'http://site2sms.com/userregistration_next.asp',
CURLOPT_REFERER => 'http://site2sms.com/UserRegistration_Next.asp',
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31',
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => http_build_query($post_fields)
);
그러나, 다음 코드를 썼다 덤프에 10002
으로 바뀌 었습니다. 그래서, 나는 오류 Warning: curl_setopt_array(): Array keys must be CURLOPT constants or equivalent integer values
을 얻고, 지금이 덤프 값
array (size=5)
'CURLOPT_URL' => string 'http://site2sms.com/userregistration_next.asp' (length=45)
'CURLOPT_REFERER' => string 'http://site2sms.com/UserRegistration_Next.asp' (length=45)
'CURLOPT_USERAGENT' => string 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31' (length=108)
'CURLOPT_POST' => boolean true
'CURLOPT_POSTFIELDS' => string 'action=UserCreate&txtFullName=fdsf&genderCombo=Male&birth_day=2&birth_month=12&birth_year=2013&txtEmail=fdsf%40dssad&ProfessCombo=1&StateCombo=Delhi&txtMobileNum=4234&cityCombo=485&Submit=Register' (length=196)
를 얻기 위해이
$options=array(
'CURLOPT_URL' => 'http://site2sms.com/userregistration_next.asp',
'CURLOPT_REFERER' => 'http://site2sms.com/UserRegistration_Next.asp',
'CURLOPT_USERAGENT' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31',
'CURLOPT_POST' => TRUE,
'CURLOPT_POSTFIELDS' => http_build_query($post_fields)
);
에 의해 원래의 배열을 교체했다. 그래서 배열을 꼬집어 어떻게 덤프 배열에 값이없는 상수 이름을 표시 할 수 있으며, curl_setopt_array
과 함께 사용하면 멋지게 작동합니다. curl_setopt_array 함수보다 한 걸음 더 빨리 사용할 수있는 함수를 찾고 있는데 배열에 필요한 변경을 할 수 있습니다. PHP 내장 함수를 통해 가능하지 않은 경우 수동으로이 함수를 생성하는 방법을 제안하십시오.
참고 :이 질문은 호기심에서 제기되었습니다. 요즘 PHP를 배우고 있습니다 –
문자열 키를 유지하면서 키를 '_setopt'에 전달하기 위해 [constant()'] (http://php.net/constant)를 활용하십시오. – mario
@mario이 배열을 배열에 어떻게 구현할 수 있습니까? 거기에 내장 된 함수가 있나요? 아니면'for each' 루프를 사용해야합니까? –