2012-03-18 22 views
5

birisi ben neredeyse benim kodunda var düşünüyorum ?PHP - data bayrağı ile kıvırmak?

curl -X POST -u "USERNAME:PASS" \ 
    -H "Content-Type: application/json" \ 
     --data '{"aps": {"alert": "this is a message"}}' \ 
      https://mywebsite.com/push/service/ 

bu linux kabuk komutu işlevselliğini yeniden üreten bir PHP komut dosyası yazabilir ama --data niteliğini nasıl işleneceğini emin değilim.

İşte benim kod şimdiye kadar gibi görünüyor:

$headers = array(); 
    $headers[] = "Content-Type: application/json"; 
    $body = '{"aps":{"alert":"this is a message"}}'; 

    $ch = curl_init(); 
    // Set the cURL options 
    curl_setopt($ch, CURLOPT_URL,   "https://mywebsite.com/push/service/"); 
    curl_setopt($ch, CURLOPT_USERPWD,  "USERNAME:PASSWORD"); 
    curl_setopt($ch, CURLOPT_POST,   TRUE); 
    curl_setopt($ch, CURLOPT_POSTFIELDS,  $body); 
    curl_setopt($ch, CURLOPT_HTTPHEADER,  $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 

    // Execute post 
    $result = curl_exec($ch); 

    // Close connection 
    curl_close($ch); 
    print_r($result); 
+1

Kodunuz var mı? iyi yatsın! – dldnh

+0

Tamam şimdi ekledim – John

cevap

1

örnek de:

http://code.google.com/apis/gdata/articles/using_cURL.html

curl https://www.google.com/accounts/ClientLogin \ 
--data-urlencode [email protected] --data-urlencode Passwd=new+foundland \ 
-d accountType=GOOGLE \ 
-d source=Google-cURL-Example \ 
-d service=lh2 
+1

Hey Zital, aslında benim kod çalışır! Sadece yazarak bir hata yaptım. Yani sadece kodumu kopyalayıp yapıştırmak ve sadece yazarak bir hata yaptım, cevabını kabul edeceğim. – John

+0

önemli değil :) – ZiTAL

2

Genel bir kural: elde etmek için "--libcurl example.c" seçeneğini kullanabilirsiniz libcurl kullanacak bir C programı için kaynak kodu oluşturmak için curl. API, göreceğiniz gibi PHP/CURL'e çok benzer ve daha sonra --data'un CURLOPT_POSTFIELDS'a çevrildiğini çabucak fark etmelisiniz.

Oh, ve -X kullanımının tamamen gereksiz olduğunu göreceksiniz! ;-)