PHP curl与--data标志? [英] PHP Curl with --data flag?

查看:126
本文介绍了PHP curl与--data标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以编写一个PHP脚本来重现这个linux shell命令的功能吗?

Can someone write a PHP script that reproduces the functionality of this linux shell command?

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

我几乎在我的代码,但我不知道如何处理 - data 属性。

I think I almost got it in my code, but I'm not sure how to handle the --data attribute.

这里是我的代码到目前为止:

Here's what my code looks like so far:

    $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);


推荐答案

example in:

example in:

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

curl https://www.google.com/accounts/ClientLogin \
--data-urlencode Email=brad.gushue@example.com --data-urlencode Passwd=new+foundland \
-d accountType=GOOGLE \
-d source=Google-cURL-Example \
-d service=lh2

这篇关于PHP curl与--data标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆