0

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.

Community
  • 1
  • 1
Nathan White
  • 65
  • 3
  • 11

3 Answers3

1

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);

curl_close($ch);
Blake Schwendiman
  • 442
  • 1
  • 4
  • 9
  • Thank you, I believe that it is working but when I try to access the Json it's supposed to return I get false or nothing? This is the json it's supposed to return `HTTP/1.1 200 OK Content-Type: application/json { "input": { "size": 207565 }, "output": { "depth": 8, "size": 63669, "ratio": 0.307, "url": "http://api.site.org/api/shrink/out/toab3elfoadgjut7.png" } }` – Nathan White Mar 13 '13 at 21:41
  • After the `curl_exec` line, you can try `print(curl_error($ch);` to see what the error is. – Blake Schwendiman Mar 13 '13 at 21:53
  • Thank you for your help but and I'am connecting to the API but it's not reconginzing the png file? I get an error message, I've tried several different solutions that I've found online and no luck. In the documentation is states that "The post data should contain the PNG binary"? Not sure if that just means a valid png file or is there a process that outputs a binary file? – Nathan White Mar 14 '13 at 20:00
  • There is another way to do it: http://stackoverflow.com/questions/4223977/send-file-via-curl-from-form-post-in-php I haven't tried it this way, but it appears with this method you can set a file pointer to one of the curl options CURLOPT_INFILE. It also shows how to set the file size and a few other possibly relevant options. – Blake Schwendiman Mar 14 '13 at 20:20
  • Thank you, I tried that and still not getting success, so frustrating. – Nathan White Mar 14 '13 at 22:05
  • Take a look at this post: http://stackoverflow.com/questions/14920475/curl-post-of-custom-binary-data-not-form-contents I'm wondering if the particular service you're using doesn't want anything at all besides the binary data in the POST. In that case, remove the `curl_setopt($ch, CURLOPT_POSTFIELDS, $data);` and try the method in the link I just added. – Blake Schwendiman Mar 15 '13 at 16:12
  • This was the issue, the service only accepts binary data. I found a solution this morning here: (http://stackoverflow.com/questions/7133140/create-a-picasa-album-and-upload-images-to-it-with-php-and-curl?rq=1) Thanks for you help though, I genuinely appreciate it. – Nathan White Mar 15 '13 at 17:26
1

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 add CURLOPT_HEADER => 1 too to the mix. Also if you don't want to output the response as of the php process's output, use CURLOPT_RETURNTRANSFER => 1 and you will get the response back from curl init.

complex857
  • 20,425
  • 6
  • 51
  • 54
0

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

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

Hope this helps. =)

Mic1780
  • 1,774
  • 9
  • 23