Setting up a JSON-RPC on my vps which I want to connect via PHP CURL on my website doing a basic request and looking for getmasternodecount.
Tried many scripts and libraries before however none seems to work in my case. Now I try to write some basic php code, but this skill isnt my best.
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
function coinFunction () {
$feed = 'http://user:pass#ip/';
$post_string = '{"method": "getmasternodecount", "params": []}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feed);
curl_setopt($ch, CURLOPT_PORT, port);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/stratum', 'Content-length: '.strlen($post_string)));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Content-length: '.strlen($post_string)));
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$data = coinFunction();
var_dump($data);
echo $data;
?>
And gives me this data dump:
string(127) "HTTP/1.1 403 Forbidden Date: Sun, 24 May 2020 00:06:21 GMT Content-Length: 0 Content-Type: text/html; charset=ISO-8859-1 " HTTP/1.1 403 Forbidden Date: Sun, 24 May 2020 00:06:21 GMT Content-Length: 0 Content-Type: text/html; charset=ISO-8859-1
When i delete all the var dump information etc, it send me a whitepage and sometimes NULL.
Kindly Regards,
Let's work with the first snippet. Since it's a POST request, file_get_contents is rather out of place here. Add the following setopt lines:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
Without those, the result of curl_exec won't contain the returned content.
It would also be advisable to specify the Content-Type of the request (which is application/json). The server might handle it even without, but just in case:
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json'));
Authentication is another thing. Credentials in the URL suggest Basic, but the server might expect otherwise... See CURLOPT_HTTPAUTH.
Related
I'm using a local php script that runs curl on a script on a remote server. The remote script is protected with http authentication.
When I run my local script, I receive the following error:
"This server could not verify that you are authorized to access the URL "/script.php". You either supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.
In case you are allowed to request the document, please check your user-id and password and try again.
If you think this is a server error, please contact the webmaster.
Error 401"
I've tried the highest-voted answers from here: How do I make a request using HTTP basic authentication with PHP curl?
and I've tried this: https://thisinterestsme.com/php-curl-http-auth/
-> I always receive the same error from above.
I have $username and $password defined. The code I'm using:
$url = 'https://' . $page . '/script.php';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additionalHeaders));
print_r($additionalHeaders);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadName);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
print_r($return);
curl_close($ch);
After printing the $additionalHeaders variable I get the following additional information before the error:
"HTTP/1.1 401 Unauthorized Date: Sat, 10 Aug 2019 22:05:26 GMT Server: Apache WWW-Authenticate: Digest realm="Protected", nonce="Xg78fMqPBQA=8d674639fbd19b9ec67b085aa43e90be7a4c57cc", algorithm=MD5, qop="auth" Vary: accept-language,accept-charset Strict-Transport-Security: max-age=16000000 Upgrade: h2 Connection: Upgrade Accept-Ranges: bytes Transfer-Encoding: chunked Content-Type: text/html; charset=utf-8 Content-Language: en"
Printing $return does not show anything ...
your website isn't using HTTP Basic Auth at all, it's using Digest access authentication, google CURLAUTH_DIGEST,
curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_DIGEST);
The working solution in its entirety (except for $url $username and $password) looks like this:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadName);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close($ch);
I'm working with this Phalcon PHP API HMAC framework and I have a little question.
I declared a GET route in routes.php and Im trying to make a request from client-connect.php (a simple CURL script to make requests).
That's how the client looks like:
$privateKey = '593fe6ed77014f9502761028801aa376f141916bd26b1b3f0271b5ec3135b989';
$time = time();
$id = 1;
$data = [
'name' => 'bob',
];
$message = buildMessage($time, $id, $data);
$hash = hash_hmac('sha256', $message, $privateKey);
$headers = ['API_ID: ' . $id, 'API_TIME: ' . $time, 'API_HASH: ' . $hash];
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_URL, $host);
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
When I make a post request, all works ok, but when I try to make a GET, PUT or DELETE request (I just uncomment the line commented in client), it returns me an error:
Request:
DELETE /nodes/mikrotik HTTP/1.1
Host: vpn.wibee.com
Accept: */*
API_ID: 1
API_TIME: 1446031137
API_HASH: 4d0852239859da5e90270b3f7dfd2167f6e5153ca83a9c5896ee262e41b19674
Content-Length: 508
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------e71b5fd144802749
Response:
HTTP/1.1 100 Continue
HTTP/1.1 401 Unauthorized
Server: nginx/1.6.2
Date: Wed, 28 Oct 2015 11:18:58 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Access denied
I think it's posible im missing something (something related with authentication). Any solution?
Thanks!
I would try it. In original implementation of client app you have such a part:
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_URL, $host);
switch($method) {
case 'POST':
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
case 'GET':
break;
default:
$data = http_build_query($data);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
break;
}
And your implementation is quite different. Once you are using POST method, functionality surprisingly work even with ($ch, CURLOPT_POST, FALSE). But Once using all other requests, you are running into troubles. It's because your implementation of GET does a few things it should not:
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
this is not correct for making GET requests with CURL. It surprisingly works, but you are receiving access denied. It is probably because authentication data, are not properly iterpreted on server side.
Also other methods will have some troubles to work, because you are mising this part of original code:
$data = http_build_query($data);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
My best solution wold be to advise you, to try to use existing code sample instead of rewriting it by hand in buggy way. Extending that code for your use should not be complicated, especially if you are running it from shell.
Here's my code for performing the POST:
$url = "http://www.xxxxxxxxxxx.com/dfeed/index.cfm"; //where to send it
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1); // set url to post to
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=ISO- 8859-1'));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 40); // times out after 4s
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // add POST fields
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch); // run the whole process
echo $result;
//echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
My post array starts out with the same few (manually defined) entries like this:
$data[0] = "<?xml version='1.0' encoding='ISO-8859-1'?>\n";
$data[1] = "<rss xmlns:g='http://base.google.com/ns/1.0' version='2.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:noNamespaceSchemaLocation='http://www.xxxxx.com/dFeed/schemas/FeedSchema1.0.xsd'>\n";
The rest of them are populated in a loop like this:
array_push($data, "\t<item>\n");
The response from the server indicates a different content-type from what I have set, and also indicates it received no information so something must be wrong:
HTTP/1.1 400 Bad Request Transfer-Encoding: chunked Content-Type: text/htmlServer: Microsoft-IIS/7.0 X-XXX Server: Web2 Date: Mon, 25 Jun 2012 12:49:53 GMT Bad Request
Error: Not XML
Content received:
The charset looks like it has whitespace in it or something, not sure if that was just on the way into SO or actually in your code.
Do you really need to add \t and \n around the place, the server won't care if you don't.
Try implode()ing the post array and just sending a string of XML.
No definitive answers but hopefully something in the right direction.
Let us know how you get on, good luck.
I'm currently trying to implement oAuth on a server side in order to provide an API for developers. I'm experiencing a very easy issue. I want to be able to handle HTTP headers sent to a script called request.php.
I have no idea how I can do that. I'm a coding a wrapper for clients, and try to make http call on request.php with curl.
$data = array('name' => 'Foo');
$header = array('Content-type: text/plain', 'Content-length: 100');
$ch = curl_init("test");
curl_setopt($ch, CURLOPT_URL, 'http://localhost/api/request.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$res = curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);
So, in $headers I received the http responses headers but what I want to do is handling headers received by request.php.
You should use
curl_setopt($s, CURLOPT_HEADER, true);
this will cause $res in you code to have both the headers and the data seperated by 2 CRLF (4 chars in total as defined in HTTP standards).
HTTP Response example,
HTTP/1.0 302 Found
Content-Type: text/html; charset=UTF-8
Content-Length: 11782
Date: Tue, 13 Dec 2011 15:07:19 GMT
Server: GFE/2.0
<!DOCTYPE html>
<html>
(...)
</html>
Use curl_getinfo to read headers.
It is not necessary to set (CURLOPT_HEADER, true) to do this.
For example:
...
curl_setopt($this->ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($this->ch);
$httpCode = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
I'm trying to post a file with curl in php, but the file is never uploaded/accepted by the server. I have searched and tried for several hours, but I can't find whats wrong, everyone elses examples and codes seems to work, but not this one.
Here is the code:
<?php
$url = "http://jpptst.ams.se/0.52/default.aspx";
$headers = array(
"Content-Type: text/xml; charset=iso-8859-1",
"Accept: text/xml"
);
$data = array("file" => "#documents/xmls/1298634571.xml");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
?>
The result I get:
string(904) "HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Mon, 25 Jul 2011 19:13:41 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 659"
Thats all I get.. the file is never accepted by the server.
If anyone can help me with this problem it would be much appreciated :)
Thanks!
You're trying to upload a file via HTTP post, so sending a Content-type: text/xml header is inappropriate. An HTTP file upload is actually done as multipart/form-data, and is actually pretty much identical to a MIME-encoded email attachment. PHP's curl will fill in the header details for you automatically. As well, the Accept header is not necessary either.
Check that the path to the .xml file you're trying to upload is correct. You've not specified a leading / to it, so the path is relative to where your PHP script is executing from.
Replace:
$data = array("file" => "#documents/xmls/1298634571.xml");
With this:
$data = array("file" => "#".realpath('documents/xmls/1298634571.xml'));
Try it, might work, i'm not sure tho.
EDIT:
Try this out:
<?php
$xmldatafile="documents/xmls/1298634571.xml"; // Make sure the file path is correct
function postData($postFields,$url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_POSTFIELDS ,$postFileds);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER ,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$xmlData = file_get_contents($xmldatafile);
$postFileds = 'data='.$xmlData;
$result = postData($postFields,"http://jpptst.ams.se/0.52/default.aspx");
?>