This is driving me crazy so hoping for some help here.
I'm looking to get an authentication token after POSTing user information to an XML page using HTTP Basic Auth via PHP. The token is supposed to be returned back in XML. I'm using curl for this.
While I get absolutely no errors when I run the PHP, I am simply unable to get the token. I just get a blank page.
Here is my PHP code:
<?php
$username ='abc';
$password = 'xyz';
$user_token = base64_encode($username . '-' . $password);
$target_url = 'https://url_example/users/sign_in.xml';
$ch = curl_init($target_url);
curl_setopt($ch, CURLOPT_POST, true);
$headers = array('Authorization=Basic ' . $user_token);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$response_data = curl_exec($ch);
$xml = simplexml_load_string($response_data);
print_r($xml);
if (curl_errno($ch)> 0){
die('There was a cURL error: ' . curl_error($ch));
} else {
curl_close($ch);
}
?>
it should be : not -
try this
$process = curl_init("https://url_example/users/sign_in.xml");
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $headers));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
curl_close($process);
Related
How can I Create, update and delete github code (file) programatically using php (preferably curl) and github api without using any external library ?
I cannot find anything about this. Is this possible or not ?
Probably the safest/most proper solution would be to use the Github API. You can find the documentation on creating a commit here: https://developer.github.com/v3/git/commits/#create-a-commit
I know you're not wanting to use a library, but this library would probably make this a lot easier: https://packagist.org/packages/knplabs/github-api
If you're not using a library, you'll need to figure out how to make sure your request is properly authenticated: https://developer.github.com/v3/#authentication
Finally I've resolved it.
<?php
function pushFile($username,$token,$repo,$branch,$path,$b64data){
$message = "Automated update";
$ch = curl_init("https://api.github.com/repos/$repo/branches/$branch");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent:Php/Automated'));
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $token);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
curl_close($ch);
$data=json_decode($data,1);
$ch2 = curl_init($data['commit']['commit']['tree']['url']);
curl_setopt($ch2, CURLOPT_HTTPHEADER, array('User-Agent:Php/Ayan Dhara'));
curl_setopt($ch2, CURLOPT_USERPWD, $username . ":" . $token);
curl_setopt($ch2, CURLOPT_TIMEOUT, 30);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, TRUE);
$data2 = curl_exec($ch2);
curl_close($ch2);
$data2=json_decode($data2,1);
$sha='';
foreach($data2["tree"] as $file)
if($file["path"]==$path)
$sha=$file["sha"];
$inputdata =[];
$inputdata["path"]=$path;
$inputdata["branch"]=$branch;
$inputdata["message"]=$message;
$inputdata["content"]=$b64data;
$inputdata["sha"]=$sha;
echo json_encode($inputdata);
$updateUrl="https://api.github.com/repos/$repo/contents/$path";
echo $updateUrl;
$ch3 = curl_init($updateUrl);
curl_setopt($ch3, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', 'User-Agent:Php/Ayan Dhara'));
curl_setopt($ch3, CURLOPT_USERPWD, $username . ":" . $token);
curl_setopt($ch3, CURLOPT_TIMEOUT, 30);
curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch3, CURLOPT_POSTFIELDS, json_encode($inputdata));
$data3 = curl_exec($ch3);
curl_close($ch3);
echo $data3;
}
//pushFile("your_username","your_personal_token","username/repository","repository_branch","path_of_targetfile_in_repository","base64_encoded_data");
?>
I followed the instructions in the official vsphere site to get info from the server and from the answer of another user here .
From what I have understood, firstly I have to get the session id (cis id), but I got "null" as a result.
I tried multiple codes but the result is the same:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = array(
'Content-Type: application/json',
'Accept: application/json',
'vmware-use-header-authn: test'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'administrator#vs.dom:userpassword');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false) {
echo 'Curl Error: ' . curl_error($ch);
exit;
}
$sid = $out;
dd($out);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/vcenter/vm");
$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);
curl_close($ch);
The result is null.
this is a script word 100% using PHP
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://{ip}/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'username' . ":" . 'password');
$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false) {
echo 'Curl Error: ' . curl_error($ch);
exit;
}
$sid = $out->value;
curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://{ip}/rest/vcenter/vm");
$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);
curl_close($ch);
?>
when you debug your code, the $out variable return null?
If yes, can you check if your curl returns 56 - OpenSSL SSL_read: Success error in $out = json_decode(curl_exec($ch));?
This error occoured with my code, and I see wich cURL in 7.67 version cause this trouble.
My solution was to downgrade cURL version to 7.65.
It works for me!
I had the same problem, and eventually understood that it is caused by this header:
curl_setopt($ch, CURLOPT_POST, true);
And replacing it with this one solves the issue:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
How can i connect to this url
curl 'https://feeds.listhub.com/pickup-v2/<your account id>/' -H 'Content-Type: Application/json' --user <your_account_id>
I replaced and in both replaces.
Here is the documentation that they have sent me
https://developer.listhub.com/faq/#how-often-should-we-update-the-feed
when i try this from php curl i get a default get of containing zip files instead of a json response
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow http 3xx redirects
$response = curl_exec($ch); // execute
// echo '<h2>API Response</h2>';
// print_r($resp_orders);
$resultStatus = curl_getinfo($ch);
if($resultStatus['http_code'] == 200) {
echo json_decode($response);
} else {
echo 'Call Failed '.print_r($resultStatus);
}
if i directly echo $response i get default webpage but i echo json_decode i get nothing..
There is no password.
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
should just be
curl_setopt($ch, CURLOPT_USERPWD, $username);
Where $username is equal to your_account_id
And you also need to set the header
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: Application/json',
));
Hope that helps.
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 have a curl code with me shown below :
curl --request GET \
--user 60CF3Ce97nRS1Z1Wp5m9kMmzHHEh8Rkuj31QCtVxjPWGYA9FymyqsK0Enm1P6mHJf0THbR:API-P4ss \
https://api.sandbox.ewaypayments.com/Transaction/44DD7aVwPYUPemGRf7pcWxyX2FJS-0Wk7xr9iE7Vatk_5vJimEbHveGSqX52B00QsBXqbLh9mGZxMHcjThQ_ITsCZ3JxKOY88WOVsFTLPrGtHRkK0E9ZDVh_Wz326QZlNlwx2
I wanted to write this code in a php file. I dnt know how to start writing this code. Can anyone tell how to write curl code in php ?
Try this:
$url = 'API URL';
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$APIresponse = curl_exec($ch);
curl_close($ch);
$res = json_decode($APIresponse, true);
// $res contains the API response as an array
}
catch(Exception $e)
{
// Error mesage
}
Reference
You need to use PHP Client URL Library.
Solution for your code:
<?php
$username = "60CF3Ce97nRS1Z1Wp5m9kMmzHHEh8Rkuj31QCtVxjPWGYA9FymyqsK0Enm1P6mHJf0THbR";
$password = "API-P4ss";
$process = curl_init("https://api.sandbox.ewaypayments.com/Transaction/44DD7aVwPYUPemGRf7pcWxyX2FJS-0Wk7xr9iE7Vatk_5vJimEbHveGSqX52B00QsBXqbLh9mGZxMHcjThQ_ITsCZ3JxKOY88WOVsFTLPrGtHRkK0E9ZDVh_Wz326QZlNlwx2");
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
curl_close($process);
var_dump($return);
The response of API is in $return variable.
First use file_get_contents php function.
If your not found solution then try below code
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));