2012-03-18 8 views
5

は、誰かがこのLinuxのシェルコマンドの機能を再現するPHPスクリプトを書くことができますか?PHPカール - データフラグ?

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

を私はほとんど私のコードでそれを得たと思いますが、私は--data属性を処理するかどうかはわかりません。

ここに私のコードは、これまでのようになります。

$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

コードがありますか?よく私たちの上にそれを置く! – dldnh

+0

今すぐ追加しました – John

答えて

1

例では:

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

実際に私のコードが動作します。ちょうど入力ミスをしました。だから、私のコードをコピーして貼り付けて、ちょうど入力ミスをどこかにしたとしたら、私はあなたの答えを受け入れるでしょう。 – John

+0

それは重要ではありません:) – ZiTAL

2

一般的なルール:取得するには、 "--libcurlはexample.c" オプションを使用しますlibcurlを使用するCプログラムのソースコードを生成するためのcurl。 APIはPHP/CURLと非常によく似ており、--dataCURLOPT_POSTFIELDSに変換されます。

ああ、あなたは-Xの使用が完全に余計であることに気づくでしょう! ;-)