简体   繁体   中英

PHP- curl isn't working?

I am trying to use an SMS Gateway API with PHP. My code is:

<?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>

This isn't working, neither it is echoing the $result . Please tell me what's wrong.

Try this way to check out your problem first and then further step forward

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

The FAQ states the following on using port 5567:

You can fall back to port 80 if you have firewall-related problems connecting to us (but we recommend that you rather take the time to allow outgoing access to ports 5567 and 7512 via your firewall). To do so, simply remove :5567 (or similar) from the URL.

Therefore, you could try the fallback port (80).

your CURLOPT_POSTFIELDS is invalid, do:

.....
$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,'&' );

and change following line:

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

to

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

Have a look at the php_curl.dll in your php.ini file, if it has a ; remove it try if it works.

You should encode POST query into string:

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

              );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM