捲曲是你所需要的:
例如,我需要把一些變量將消息發送文本電話:
<?php
$phoneNumber = '4045551111';
$message = 'This message was generated by curl and php';
$curlPost = 'pNUMBER=' . urlencode($phoneNumber) . '&MESSAGE=' . urlencode($message) . '&SUBMIT=Send';
// initialize connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.webserver.com/sendSMS.php');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
//execute post
$data = curl_exec();
curl_close($ch);
或者您可以通過JSON以正確格式的JSON數據發送數據:
<?php
$data = array("phoneNumber" => "4045551111", "message" => "This message was generated by curl and php");
$data_string = json_encode($data);
// initialize connection
$ch = curl_init('http://www.webserver.com/sendSMS.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
//execute post
$result = curl_exec($ch);
curl_close($ch);
CURLOPT_RETURNTRANSFER
純粹是這樣,來自遠程服務器的響應放置在$result
而不是回顯。
停止使用'@'來抑制您的錯誤消息。抑制錯誤的函數可能是問題,並且你壓制的錯誤會告訴你爲什麼。 – Sammitch 2014-09-25 16:26:59
我的錯誤實際上在於代碼中的其他地方,但下面的curl方法比我目前的更好。 – Aurora 2014-09-25 16:37:34