Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Curl with --data flag?

Tags:

linux

php

curl

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/

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);
like image 317
John Avatar asked Mar 18 '12 22:03

John


People also ask

What is the flag in cURL?

What is a flag in Curl? A flag is a command-line parameter that denotes a specific action in Curl. Curl has over three hundred command-line options, and the number of options increases over time.

How pass JSON data in cURL in PHP?

Send JSON data via POST with PHP cURLInitiate new cURL resource using curl_init(). Setup data in PHP array and encode into a JSON string using json_encode(). Attach JSON data to the POST fields using the CURLOPT_POSTFIELDS option. Set the Content-Type of request to application/json using the CURLOPT_HTTPHEADER option.

What is cURL setopt?

curl setopt($ch, option, value) sets a cURL session option defined by the ch parameter. The value specifies the value for the specified option, and the option specifies which option to set. Return page contents with curl setopt($ch, CURLOPT RETURNTRANSFER, 1). If the value is zero, no output will be returned.

What is the use of Curlopt_returntransfer?

When CURLOPT_RETURNTRANSFER is set to true the whole fetched content is held in memory when set to false you only have the unechoed buffer in memory. To verify just try to fetch something very small.


2 Answers

example in:

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
like image 85
ZiTAL Avatar answered Nov 04 '22 23:11

ZiTAL


A general rule: use the "--libcurl example.c" option to get curl to generate source code for a C program that would use libcurl. The API is very similar to the PHP/CURL one as you will see and you should then quickly realize that --data translates to CURLOPT_POSTFIELDS.

Oh, and you'll note that the -X usage is completely superfluous! ;-)

like image 26
Daniel Stenberg Avatar answered Nov 05 '22 00:11

Daniel Stenberg