短信网关为每个错误


Sms gateway foreach error

任何人都可以帮帮我吗?我想为 SMS 网关创建 php 脚本,但 foreach 命令有问题。我找不到什么...

这是我的代码:

<?php 
    $url = 'http://www.freesmsgateway.com/api_send';
    $post_contacts = array('381645391312', '0646395732', '99381625597222'); //phone numbers  xxxxxxxxxx format
    $json_contacts = json_encode($post_contacts); //encode them to json 
    $fields = array(
                'access_token'=>'53f86041s38544544e482b956bcac006',
                'message'=>urlencode('Test sms poruka'),
                'send_to'=>'post_contacts', //existing_contacts or post_contacts
                'post_contacts'=>urlencode($json_contacts),
                );
    $fields_string = '';
    foreach($fields_string as $key=>$value) { $fields_string .= $key.'='.$value.'&';}
    //rtrim($fields_string,'&');
    //open connection
    $ch = curl_init();
    //set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST,count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); //optional
    //execute post
    $result = curl_exec($ch);
    //close connection
    curl_close($ch);
    print $result; //optional

    ?>

我有这个错误

Warning: Invalid argument supplied for foreach() in /home/user/public_html/sms/smsfreegateway.php on line 15
 The message was blank.
foreach($fields_string as $key=>$value)

$fields_string是一个字符串,而不是一个数组,你刚刚将其初始化为空。也许你的意思是在这里迭代$fields