2013-09-07 2 views
-1

아무도 도와주세요. SMS 게이트웨이 용 PHP 스크립트를 만들고 싶지만 foreach 명령에 문제가 있습니다. 당신은SMS 게이트웨이 오류 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
foreach($fields_string as $key=>$value) 

$fields_string가이 오류를 문자열이 아닌 배열을 가지고 있고 : 나는 무엇을 찾을 수 없습니다 ... 여기

내 코드입니다 방금 비어있는 것으로 초기화했습니다. 아마도 $fields을 반복 할 의향이 있습니까?

+0

저는 PHP에 익숙하지 않지만 스크립트는 $ fields 배열을 사용하고 별도로 변수를 가져야합니다. $ post_contacts를 사용하여 번호로 메시지를 보냅니다. 이것은 api 용 개발자 섹션의 코드입니다. 이 코드를 작동시키는 방법은 무엇입니까? – Aleksandar

+0

어 ... 맞아! 사실,'$ fields'는 아마도 $ fields_string이 아닌 반복 할 작업 일 것입니다. – duskwuff

+0

'$ fields_string = ''; foreach ($ fields => $ value) {$ fields. = $ key. '='. $ value. '&';} ' 오류가 없습니다. 이 올바른지? – Aleksandar