api - How to do I format this curl command in PHP


I'am trying build a PHP call to my first API and the example request shown in the docs, tell me to make this request with curl but I'am not having much success. I've tried to follow other answers (i.e. CURL php query formation - how to? ) and know i'am doing something wrong but can't seem to figure it out?

Any help would be greatly appreciated.

The initial request must be authorized with HTTP Basic authorization.

 POST /api/shrink HTTP/1.1
 Host: api.site.org
 Authorization: Basic YXsdflasdkfjalsdjfojiosslkjdZdXZ3eHl6MDEyMzQ1  

curl -i --user api:api_key --data-binary @test.png http://api.site.org

This should return a json string that includes a url to an image file.




Should be something like this:

$url = "http://api.site.org/api/shrink";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); /* obey redirects */
curl_setopt($ch, CURLOPT_HEADER, 0);  /* No HTTP headers */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  /* return the data */
curl_setopt($process, CURLOPT_USERPWD, 'api:api_key');

$data = array(
    'uploaded_file' => '@/path/to/test.png'
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$result = curl_exec($ch);




I think it would go something like this:

$c = curl_init('http://api.site.org');
curl_setopt_array($c, array(
    CURLOPT_USERPWD => 'api:api_key' // --user
    CURLOPT_POST    => 1,
    CURLOPT_POSTFIELDS => '@/full/path/to/test.png;type=image/png', // --data-binary, added mime type for good measure
    CURLOPT_HEADER => 1, // this is to the -i
    CURLOPT_RETURNTRANSFER => 1, // so you get the response back from curl_exec()
$response = curl_exec($c);

You have the-i flag, but i'm not sure if you want to have the response header, in case you do addCURLOPT_HEADER => 1 too to the mix. Also if you don't want to output the response as of the php process's output, useCURLOPT_RETURNTRANSFER => 1 and you will get the response back from curl init.




Looks to me that all the information you need can be found here

The cURL functioncurl_setopt allows you to set your curl options that best suit you.

Hope this helps. =)

People are also looking for solutions to the problem: php - Execute tcl script from website as different users


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.