-1

任何人都可以帮助我。我想为短信网关创建 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.
4

1 回答 1

0
foreach($fields_string as $key=>$value)

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

于 2013-09-07T20:50:17.817 回答