PHP Curl with --Data Flag

PHP Curl with --data flag?

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

Curl -H flag in php

-H is a header for the curl command line, so why are you setting them as the postfields?

Seems like it should be:

$headers =  array('mail: mail@api.com', 'password: XXX');

This should be the same as your CLI command above. If this doesn't work, check the output of curl_exec() to see what the server is responding.

using curl -G flag in php

For posting the data(you can use array though):

$request = 'where={"playerName":"Jonathan Walsh"}&count=1&limit=0';
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$request);

For https:// in your url

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

To return the date from curl execution

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

Finally:

$result = curl_exec($ch);

UPDATE:

Didn't check that you are doing GET operation. If you do GET, then it will be simply

$request = 'where={"playerName":"Jonathan Walsh"}&count=1&limit=0';
$url = "http://example.com/" . "?" . $request;
curl_setopt($ch, CURLOPT_URL,$url);

PHP Curl with flag for username/password

Have you tried sending basic authorization?

curl_setopt($ch, CURLOPT_URL, "https://url.desk.com/api/v2/cases");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HEADER, 'Accept: application/json');

You also won't need the '-u' in your url, that is meant for command line curl.

Additionally, You'll need to replace ':id' with the ID of the case for the above to return a specific case. For example:

curl_setopt($ch, CURLOPT_URL, "https://url.desk.com/api/v2/cases/1");

How to send raw data with curl GET in PHP?

GET requests do not have a body, that's the whole idea: you're just getting something from the server, as opposed to posting something to it. From RFC 7231:

A payload within a GET request message has no defined semantics;
sending a payload body on a GET request might cause some existing
implementations to reject the request.

In other words, a GET request can have data, but it should not. From earlier in the spec, where GET is defined as a safe method:

Request methods are considered "safe" if their defined semantics are
essentially read-only; i.e., the client does not request, and does
not expect, any state change on the origin server as a result of
applying a safe method to a target resource.

...

Of the request methods defined by this specification, the GET, HEAD,
OPTIONS, and TRACE methods are defined to be safe.

If you really want to have JSON in your GET request (and send it to a reasonably implemented server resource) the only place it can go is in the URI as part of the query string. For GET requests I find using file_get_contents to be much easier than dealing with cURL.

<?php
$payload = json_encode(["user" => $data]);
$url_data = http_build_query([
"json" => $payload
]);
$url = "https://some.example/endpoint.php?" . $url_data;

$result = file_get_contents($url);

If you want to send it to an unreasonably implemented server resource, and violate the spirit of the HTTP RFCs, you could do this:

<?php
$url = "https://some.example/endpoint.php";
$payload = json_encode(["user" => $data]);
$ctx = stream_context_create(["http" => [
"header"=>"Content-Type: application/json",
"content"=>$payload
]]);
$result = file_get_contents($url, false, $ctx);

If you're determined to do this specifically with cURL, you might have luck with the CURLOPT_CUSTOMREQUEST option set to "GET" and CURLOPT_POSTDATA with your data.

In PHP how to send signed xml as soap request using curl

Code
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
replaced with
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('soapRequest.xml'))

And it started working fine.

Instead of specifying value of file in $message variable and then setting $message in CURLOPT_POSTFIELDS, now using file_get_contents() to read file and set CURLOPT_POSTFIELDS.

Taken hint from PHP cURL: how to set body to binary data?

PHP cURL: how to set body to binary data?

You can just set your body in CURLOPT_POSTFIELDS.

Example:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://url/url/url" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, "body goes here" );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));

$result=curl_exec ($ch);

Taken from here

Of course, set your own header type, and just do file_get_contents('/path/to/file') for body.



Related Topics



Leave a reply



Submit