The task seemed simple.
The login (email) used for authentication must be passed in the login request parameter. In the request body (Body), the user password is passed as a string encoded in UTF-8.
Example request:
POST /auth/authenticate-by-pass?login=testlogin#testDomain.net HTTP/1.1
Host: somehost.ru
Body: somepassword
Cache-Control: no-cache
If the request is successful, the response will contain a JSON object
Tried to do like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://somehost.ru/auth/authenticate-by-pass?login=mylogin#mydomain.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, "mypassword" );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain', 'Host: somehost.ru'));
$result=curl_exec($ch);
$php_obj = json_decode($result, true);
print_r($php_obj);
No result. Nothing is displayed. Please help.
In my understanding what you need can be simply (please amend the headers to further suit your needs if necessary) :
$ch = curl_init();
$post = [
'password' => 'xxxxxx'
];
curl_setopt($ch, CURLOPT_URL, 'http://somehost.ru/auth/authenticate-by-pass?login=mylogin#mydomain.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$php_obj = json_decode($result, true);
print_r($php_obj);
curl_close($ch);
Solved the problem. Ken Lee and everyone else thanks for the help.
$ch = curl_init();
$post = 'mypassword';
curl_setopt($ch, CURLOPT_URL, 'http://somehost.ru/auth/authenticate-by-pass?login=mylogin#mydomain.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$php_obj = json_decode($result, true);
print_r($php_obj);
curl_close($ch);
Related
I would like to upload a textfile to Google Cloud Storage.
I use this function in php:
function upload_object_to_Google_bucket($bucket,$accesstoken,$proxy,$objectname,$objectpath)
{
#Build the CUrl: incarnate.github.io/curl-to-php
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, "https://storage.googleapis.com/upload/storage/v1/b/".$bucket."/o?uploadType=media&name=".$objectname);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
# curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
$file=$objectpath.$objectname;
$item=make_curl_file($file);
print_r($item);
$file = $objectpath.$objectname;
$post = array(
$item
);
# curl_setopt($ch, CURLOPT_UPLOAD, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
# curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
# curl_setopt($ch, CURLOPT_NOBODY, true);
$headers = array();
$headers[] = 'Authorization: Bearer '.$accesstoken;
$headers[] = 'Content-Type: '.object_to_array($item)["mime"];
$headers[] = 'Content-Length: '.filesize($file);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
print_r($response);
if (curl_errno($ch)){
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
}
It's all fine, but the textfile has become new content in the Bucket.
--------------------------9ad622dfd933f230
Content-Disposition: form-data; name="0"; filename="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xml"
Content-Type: text/plain
These lines are added to the file. Does anyone has an idea whats the problem and how I can avoid this behaviour?
The cURL request I am trying to translate is similar to this:
curl -0 -XPOST -u user:pass --data '{"jsonrpc":"2.0","method":"retrieve_summary_info","params":[true, 10],"id":1}' http://127.0.0.1:3141/v2/owner
I want to send this with php and then display the json response but am not sure how I would translate that to PHP
Try this code:
$ch = curl_init();
$data = "{\"jsonrpc\":\"2.0\",\"method\":\"retrieve_summary_info\",\"params\":[true, 10],\"id\":1}";
$user = ''; // set your user
$pass = ''; // set your password
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:3141/v2/owner');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "{$user}:{$pass}");
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
Check this website, it will help you
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:3141/v2/owner');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"jsonrpc\":\"2.0\",\"method\":\"retrieve_summary_info\",\"params\":[true, 10],\"id\":1}");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'user' . ':' . 'pass');
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
I'm making cURL request to file on my server And it's printing empty array, unless I delete Content-Type: application/json header from request. Why it occure? It should expect json format in return....
$o = [ 'key' => 'value123' ];
$headers[] = 'Content-Type: application/json';
$headers[] = 'Moj-pierwszy-header: prosty/do/zapamietania';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path.'test.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $o);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo( $output );
and the test.php
echo json_encode( $_POST );
Anybody? Any ideas?
Please refer the below code snippet:
$o = json_encode([ 'key' => 'value123' ]);
$headers[] = 'Content-Type: application/json';
$headers[] = 'Moj-pierwszy-header: prosty/do/zapamietania';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path.'test.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $o);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo( $output );
and for output, instead of $_POST use below code snippet:
$post = json_decode(file_get_contents('php://input'));
Hope, it'll work for you.
So i'm using cURL with PHP to retrieve some data from a JSON file. The cURL script returns something like this: https://s4.postimg.org/43svl8rot/image.png
I'm using this PHP script:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://test.pt/api/objgroupinfo/16Jcr05g37KpLklz");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "X-Apikey: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://test.pt/api/dataout/IAfhAfTIUZrCje5q.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "X-Apikey: xxxxxxxxxxxxxxxxxxxxxx";
$headers[] = "X-Startdate: 2016-10-01 00:00:00";
$headers[] = "X-Enddate: 2016-10-10 15:00:00";
$headers[] = "X-Channelnum: 0";
$headers[] = "X-Reclimit: 200";
$headers[] = "User-Agent: test/1.0";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$json = json_decode($result,true);
print_R($json);
curl_close ($ch);
Can someone help me on how to turn this data into an object or array that i can use? I need to acess this data to show it graphically
json_decode will convert a JSON encoded string to a PHP variable, see json_decode. It looks like your code is already doing this.
This is the curl, php program i gave to retrieve data from flip kart using there API. But what ever i do , i am getting the same error as :
{"error":"unauthorized","error_description":"An Authentication object was not found in the SecurityContext"}
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.flipkart.net/oauth-service/oauth/token?grant_type=client_credentials&scope=Seller_Api,Default");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers[] = 'appid:appsecret';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
print $server_output ;
curl_close ($ch);
?>
I dont know what is the error , i checked number of time if the problem is with appid and appsecret i gave but still its showing same error.
Flipkart Documentation Link
$action = 'https://sandbox-api.flipkart.net/oauth-service/oauth/token?grant_type=client_credentials&scope=Seller_Api';
$headers = array();
$headers[] = 'content-type: application/json';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $action);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, "<id>:<key>");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
$result = curl_exec($curl);
echo '<pre>';print_r($result);
curl_close($curl);