Search code examples
phppostnamespacesunirest

Unirest POST request won't run in PHP


I have a block of code that's almost as barebones as it gets, but for some reason, it just refuses to run:

<?php
require_once "unirest/src/Unirest.php";

$photo_url = "http://api.animetrics.com/img/test/sc.jpg";
// These code snippets use an open-source library.
$response = Unirest::post("<--URL-->",
  array(
    "X-Mashape-Key" => "<--API Key-->",
    "Content-Type" => "application/x-www-form-urlencoded",
    "Accept" => "application/json"
  ),
  array(
    "selector" => "FACE, EYES, FULL",
    "url" => "http://api.animetrics.com/img/test/sc.jpg"
  )
);
echo $response;
?>

This code block was taken directly from the mashape website, and I simply downloaded the Unirest files. I'm also sure that my path is correct.

enter image description here

I investigated a bit and tried adding a static class function into the Unirest file to print something out, and unsurprisingly, it didn't work.

Here's the code chunk I added:

<?php

namespace Unirest;

echo "in file";

$file = new File(); 

$file->printa("abc");

class File
{

    public static function printa($a) {
        echo $a;
    } 
....

Within the same file, $file->printa("abc"); worked perfectly, but when called from a different file, File::printa("abc"); or Unirest::printa("abc"); or File\Unirest::printa("abc"); just refuse to run.

I'm not sure but am I misunderstanding something about namespaces? I would have thought that Unirest::printa("abc"); is the correct way to access a static class function?

I'd appreciate any advice regarding this, thanks.


Solution

  • Since the release of Unirest 2.0, the method & class signature has changed. unfortunately the Mashape sample snippets are yet to be updated.

    Instead of calling Unirest::post you should be calling Unirest\Request::post, please refer to the unirest documentation for more details.

    We'll be updating the Mashape samples soon to reflect this change.

    I'm the author of unirest-php and I work at Mashape.