如何设置cURL以从其他站点检索数据

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.