I got stuck while trying to set up cURL to retrieve data from other site(s)
Here is my situation.
I have 2 websites :
A
B
Website A sent data to website B as json format.
Of course, website A will have to encode all of it data before sending out - that's done.
Let the :
username = test
password = 1234
Website B just need run this command
curl --user test:1234 http://localhost/api/
They will then get the json file, and make anything out of it.
But, what if I have to do a mutiple cURL request.
SO I want to write a php script to do that.
This is what I have so far :
<?php
$ch = curl_init("http://localhost/api/");
$fp = fopen("api.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
Questions
While doing it this way, I am not sure how to configure the username and password.
After, knowing where to set the username and password, where should I do the decoding of the json ?
After that, how do I display those data that I just decoded in a HTML/PHP format ?
How do I test it ?
Set the username and password
Website B is using HTTP Basic Authentication. This is a authentication method via HTTP headers. You'll have to set the username and password in the Authorization header. This can be done With the cUrl module for PHP like this:
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
Decode your result
Assuming you just need the JSON data that website B is returning. Use the following option. It will make curl_exec() return the HTTP body instead of outputting it directly.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
For the list of options that can be used with the PHP curl module check the documentation on curl_setopt(). Now you are ready to make your request with cUrl using curl_exec() like this:
$body = curl_exec($ch);
Then decode the JSON data, for the sake of simplicity lets decode it to a PHP array:
$data = json_decode($body, true);
Test your code
In my opinion PHP is not a language which offers great testing features. Putting that aside, most of the time I test my code in an interactive shell. To start testing your scenario in an interactive shell you should have PHP CLI installed on your system. In linux this is straight forward. Start an interactive PHP shell with the following command:
php -a
Now you can start putting together your scenario.
php > $ch = curl_init('http://echo.jsontest.com/key/value/one/two');
php > curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
php > $body = curl_exec($ch);
php > var_dump($body);
string(39) "{
"one": "two",
"key": "value"
}
"
php > $data = json_decode($body, true);
php > print_r($data);
Array
(
[one] => two
[key] => value
)
You can configure username and password like this
//cURL Options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => 'test:1234', // username:test pass:1234
CURLOPT_HTTPHEADER => array(‘Content-type: application/json’) ,
CURLOPT_POSTFIELDS => $json_string
);
// Setting curl options like this
curl_setopt_array( $ch, $options );
// Getting results
$result = curl_exec($ch); // Getting jSON result string
How do you test it?
I would put all that in a .php file and call it on the browser to debug to keep it simple.
Related
I am trying to connect to an API with Powershell using Invoke-RestMethod, but I keep running into an error for invalid credentials. I am converting the -Body to JSON in my API call. The documentation states there is no header and only 2 parameters: the username and password. I am also using -ContentType application/json, although I believe the issue lies with the JSON and not the Powershell itself.
The API has a test on their website where you can input your credentials to get an API key, which works fine so I know the credentials are correct. They also have a textbox to test authentication with JSON, which throws an error. The JSON I am testing looks like this:
{
"username": "myusername",
"password": "xxxxxxxx"
}
The API documentation also provides a sample PHP script to get an API key, which I confirmed does work when I put my credentials into it. The sample script is below:
$url = "https:website.com/api";
$content = ["username"=>"myusername","password"=>"********"];
$curl = curl_init($url);
if($curl) {
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// POST method
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($content));
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$response = json_decode($json_response, true);
} else {
die("failed to connect\n");
}
I am not a PHP developer, but after looking up every command I believe I understand what it is doing for the most part. I am just wondering if I am missing anything in my JSON that the PHP script might add. In the PHP script I did convert the $content variable to JSON and it is identical to the JSON provided above, as well as the JSON used in the Powershell command.
As a side note, I have also tested it with with a SecureString in Powershell, which yielded the same results. Once I get the JSON to work I am planning on using a SecureString.
Any kind of assistance would be very helpful. Thank you!
I have two php scripts on two different servers. I'm using curl to get data from one to the other (from server 1 to server 2):
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
It works but is there a way to hide the output on server 1 if somebody is accessing that script through its actual url?
A very basic way to lock down your PHP script is to create a password via a URL parameter. What if you had ?pwd=ABCDEF (create your own password here https://passwordsgenerator.net/) and then your Server-side PHP script can check to see if the password matches the URL parameter, and if it does you can print out the output.
Then modify PHP script #2 to pass your chosen password as a URL Parameter
I've been going round in circles trying to get this bit of code working. The problem I am facing is that there could be any number of places where something is wrong and I'm not experienced enough with cURL and API requests to know if I've just done something simple and silly somewhere. The code below is supposed to fetch a JSON response. What I am currently getting is "false". The API developer keeps giving me a CLI sample and I don't know how to "translate" that into something I can use in PHP.
I have to hide the domain, service name and authentication details in my examples.
The string I was given:
'https://[domain]/agw/latest/services/[service]-api/latest/api/v2/[service]-actual-prizes -vk -H "Proxy-Authorization: Basic [authstr]"'
([authstr] is the username and password, separated by a colon and BASE64 encoded - the API dev has confirmed that my authorisation string is correct)
What I have been trying:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://[domain]/agw/latest/services/lottery-api/latest/api/v2/sportka-actual-prizes');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Proxy-Authorization: Basic '.$authstr.'"
,"Content-type: application/json"
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
If I understand this correctly (and I'm not sure that I do), then I'm passing the URL (without flags), saying that I don't want a header in the response (I've tried TRUE as well without any success) and then passing headers with my request that includes the authorisation.
I've tried file_get_contents with a stream_context_create header but that fails too.
Am I missing a header option or flag or something in my cURL code?
Is it possible to send a multi-associative array to a page using cURL in php?
I am able to pass an array, but the following happens:
// Open Connection
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, $this->config['submission']['eyerys']);
// Set the number of fields being sent:
curl_setopt($ch,CURLOPT_POST,count($this->call['info']));
// The string to send:
curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
// Return transfer:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// SSL verification:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// Execute the post:
$result = curl_exec($ch);
$this->pre($result);
// Close connection:
$curl_close($ch);
I get the following output:
Array
(
[info] => Array
[answers] => Array
[errors] => Array
)
Nope, since curl cannot know how you want to encode it. Not every server-side language/framework uses the same way. I think PHP is the only language where the user can create an array by simply sending data with keys containing []. For example. in the python world one would just send the same value twice and then use a different function (such as .getlist('key') - depends on the framework though) to access the array instead of just a single value.
If you have control over the remote script, consider using something standardized such as JSON. Instead of sending a formencoded POST string either send a pure JSON body or a single formencoded POST value containing the JSON.
If you don't, you'll most likely have to encode the POST data on your own.
I'm attempting to access an API for a task management system (Nozbe to be exact) that is outlined here: http://www.nozbe.com/api
If I go in my browser and access the URL it returns the correct json response:
{"response":"644a40436"}
However, when I attempt to access this URL with cURL in PHP, it doesn't create the note like it would if I accessed it manually in my browser.
The normal method is outlined below:
http://www.nozbe.com/api/newnote/name-test/body-test/project_id-c4ca1/context_id-c4ca1/key-1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6
$api_key = "INSERTAPIKEYHERE";
$project_id = "73d173457";
$eventtitle = "Testing";
$descrip = "This is a test";
$url = "http://www.nozbe.com/api/newnote/name-$eventtitle/body-$descrip/project_id-$project_id/key-$api_key";
echo "$url<br/><br/>";
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
// grab URL and pass it to the browser
$response = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
Does anyone have any advice or pointers as to why this isn't working? I know it's probably a pretty obscure API I'm attempting to access.
Note that the API says that the body and the like must be URL-encoded. Instead, you have spaces in your URL. Try running urlencode on the arguments before placing them into the URL.