2017-12-13 5 views
-1

PHP에서 curl을 사용하여 양식을 제출하려고합니다. 대상 URL의 필드 이름을 가져 와서 내 필드에 매핑 할 수있는 웹이 있습니까? 다음은 내 코드가, 나는 컬을 위해 사용하고 있습니다 :PHP에서 컬을 사용하여 양식 제출

<form action="" method="post"> 
<input type="text" name="name" > 
<input type="text" name="email"> 
<input type="submit" name ="create_account"> 
</form> 


<?php 
if(isset($_POST['create_account'])){ 
$name = $_POST['name']; 
$email = $_POST['email']; 
$url = "www.exampleurl.com"; // Where you want to post data 
try { 
    $ch = curl_init(); 

    if (FALSE === $ch) 
    throw new Exception('failed to initialize'); 

    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, true); // Tell cURL you want to post something 
    curl_setopt($ch, CURLOPT_POSTFIELDS, "name=". $name ."&email=". $email); 
    // Define what you want to post 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    //curl_setopt(/* ... */); 
    $content = curl_exec($ch); 
    $info = curl_getinfo($ch); 
    $json = json_decode($content, true); 
    if (FALSE === $content) 
    throw new Exception(curl_error($ch), curl_errno($ch)); 

    } catch(Exception $e) { 

    trigger_error(sprintf(
    'Curl failed with error #%d: %s', 
    $e->getCode(), $e->getMessage()), 
    E_USER_ERROR); 
    } 
} 
?> 
+0

오류가 발생했습니다. – Camille

+0

내 필드를 대상 URL 필드에 매핑 할 수 없습니다. – Archana

+0

매핑이'CURLOPT_POSTFIELDS'로 완료되었으므로 작동합니다. HTML 양식을 표시하는 url이 제출 된 것과 동일하다는 것을 확신하십니까 (form 태그의 action 매개 변수) – Camille

답변

1

이 시도 : PHP5에서

$data = array('name'=>$name, 'email'=>$email); 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); 

위쪽, http_build_query의 사용이 권장됩니다.

php5 이전에는 각 var 값에 대해 urlencode()를 수행 할 수있었습니다.