2014-02-06 27 views
-2

昨天和今天我一直在努力解決這個問題。沒有合適的答案在這個論壇上,我可以申請。我以前使用過的代碼,它的工作完美,我不知道我做錯了什麼。當我在瀏覽器中輸入url和值時,它可以工作,但curl函數不能。當我檢查錯誤時,它給了我信息:0,但它仍然無效或發佈我的價值。php curl只適用於瀏覽器。爲什麼?

這裏是我的代碼

$contact_phone = $_POST['contact_phone']; 
    $message = $_POST['message']; 

    function sendMessage($contact_phone, $message) { 

     $postValue = "sender=$contact_phone&message=$message&pass=***"; 


     $apiUrl = "http://mydomain.com/components/com_simserver/sim_api.php?"; 
    //next is to fake a browser form submitted 
    //firefox is our choosing browser 
     $browserType = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)"; 

    /initiating the curl library 
    $ci = curl_init(); 
curl_setopt($ci,CURLOPT_FAILONERROR,true); 
//set the url to be used for processing the data 
    curl_setopt($ci, CURLOPT_URL, $apiUrl); 

//set our browser type that has been initiad before 
    curl_setopt($ci, CURLOPT_USERAGENT, $browserType); 

//set the maxmium time to execute the script before timing out 
    curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 0); 

//accept the response after the execution 
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true); 

// set the post method for passing variables to the server 
    curl_setopt($ci, CURLOPT_POST, 1); 

//assigning the values to be posted to the sms gateway 
    curl_setopt($ci, CURLOPT_POSTFIELDS, $postValue); 

//execute the function and get result from the gateway 
    $gatewayResult = curl_exec($ci); 

echo "Info: " . print_r(curl_error($ci)) . "\n"; 
    curl_close($ci); // close the connection 

    return $gatewayResult; //returning the gateway feedback 

    /* return "Message sent"; */ 
} 


    $smsGateWay = sendMessage($contact_phone, $message); 
        if (isset($smsGateWay)) 
       { 
        echo "message sent" 
        } 

    ?> 
    <form method="post"> 
    <input type="text" name="contact_phone" /> 
    <input type="text" name="message" /> 
    <input type="submit" value"submit" /> 
    </form> 
+0

你得到的錯誤是什麼?你不清楚。 – Zarathuztra

+0

「info:1」正在回顯print_r函數的返回值,對嗎?如果你想要它做我認爲你期望的,你需要 'echo「Info:」。 print_r(curl_error($ ci),TRUE)。 「\ n」;' 至少可以讓你知道真正的錯誤。 – Andrew

回答

0

我已經解決了這個問題。 API/URL不接受發佈的值,因此我必須包含CURLOPT_URL上的所有值,例如: $apiUrl = "http://mydomain.com/components/com_simserver/sim_api.php?&sender=$contact_phone&message=$message&pass=***";

相關問題