簡體   English   中英

PHP- curl無法正常工作?

[英]PHP- curl isn't working?

我正在嘗試通過PHP使用SMS網關API。 我的代碼是:

<?php
 // API integration
 if (isset($_POST['submit'])) {

     //Variables to POST
     $username = "user";
     $password = "pass";
     $msisdn = $_POST['phone'];
     $message = $_POST['message'];

     //Initialize CURL data to send via POST to the API
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, "http://bulksms.vsms.net:5567/eapi/submission/send_sms/2/2.0");
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS,
              array('username' => $username,
                    'password' => $password,
                    'message' => $message,
                    'msisdn' => $msisdn)
              );

     //Execute CURL command and return into variable $result

     $result = curl_exec($ch);
     echo "$result";
 }
?>
 <form name='sms' action='' method='post'>
 Phone number (eg.: 919999999999, separated by commas) <br/><input type='text' name='phone' value=''/>
 Message: <br/><input type='textarea' name='message' value=''/> 
 <br/>
 <input type='submit' name='submit' value='Send SMS'>
 </form>

這是行不通的,也不是在呼應$result 請告訴我怎么了。

嘗試這種方式先檢查您的問題,然后再進一步前進

$error=curl_error($ch);    //print_r($error); 
$header=curl_getinfo( $ch ); //print_r($header);
$result = curl_exec($ch);  //print_r($result);

FAQ在使用端口5567時指出以下內容:

如果您遇到與防火牆相關的問題,可以退回到端口80(但我們建議您花一些時間允許通過防火牆對端口5567和7512進行出站訪問)。 為此,只需從URL中刪除:5567(或類似名稱)。

因此,您可以嘗試使用備用端口(80)。

您的CURLOPT_POSTFIELDS無效,請執行以下操作:

.....
$postData = array('username' => $username,
    'password' => $password,
    'message' => $message,
    'msisdn' => $msisdn
);

//create post body  
$post_body = '';
foreach( $postData as $key => $value ) {
    $post_body .= urlencode( $key ).'='.urlencode( $value ).'&';
}
$post_body = rtrim( $post_body,'&' );

並更改以下行:

...
curl_setopt($ch, CURLOPT_POSTFIELDS,...);
...

..
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_body);
...
echo $result;

看一下php.ini文件中的php_curl.dll(如果帶有); 刪除它,如果可行。

您應該將POST查詢編碼為字符串:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('username' => $username,
                    'password' => $password,
                    'message' => $message,
                    'msisdn' => $msisdn), '', '&');

              );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM