任何人都可以帮助我。 我想为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.
答案 0 :(得分:0)
foreach($fields_string as $key=>$value)
$fields_string
是一个字符串,而不是一个数组,你刚刚将它初始化为空。也许你想在这里迭代$fields
?