昨天和今天我一直在努力解决这个问题。没有合适的答案在这个论坛上,我可以申请。我以前使用过的代码,它的工作完美,我不知道我做错了什么。当我在浏览器中输入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>
你得到的错误是什么?你不清楚。 – Zarathuztra
“info:1”正在回显print_r函数的返回值,对吗?如果你想要它做我认为你期望的,你需要 'echo“Info:”。 print_r(curl_error($ ci),TRUE)。 “\ n”;' 至少可以让你知道真正的错误。 – Andrew