[GH-ISSUE #105] Can't access to json Obj when uploading an image #80

Closed
opened 2026-02-25 23:40:37 +03:00 by kerem · 4 comments
Owner

Originally created by @vitalijalbu on GitHub (Jan 21, 2020).
Original GitHub issue: https://github.com/HaschekSolutions/pictshare/issues/105

When i try to upload an image i would like to get only hash ID and save it into DB.
But response object can't be accessible.

Originally created by @vitalijalbu on GitHub (Jan 21, 2020). Original GitHub issue: https://github.com/HaschekSolutions/pictshare/issues/105 When i try to upload an image i would like to get only hash ID and save it into DB. But response object can't be accessible.
kerem closed this issue 2026-02-25 23:40:37 +03:00
Author
Owner

@geek-at commented on GitHub (Jan 21, 2020):

what do you mean you can't access the json object? When you upload an image

eg via curl: curl -s -F "file=@/path/to/your/image.jpg" https://pictshare.net/api/upload.php you get the answer in form of a JSON object that defines the hash and path.

{
  "status": "ok",
  "hash": "e17o3a.jpg",
  "filetype": "jpeg",
  "url": "https://pictshare.net/e17o3a.jpg"
}
<!-- gh-comment-id:576696872 --> @geek-at commented on GitHub (Jan 21, 2020): what do you mean you can't access the json object? When you upload an image eg via curl: `curl -s -F "file=@/path/to/your/image.jpg" https://pictshare.net/api/upload.php` you get the answer in form of a JSON object that defines the hash and path. ```json { "status": "ok", "hash": "e17o3a.jpg", "filetype": "jpeg", "url": "https://pictshare.net/e17o3a.jpg" } ```
Author
Owner

@vitalijalbu commented on GitHub (Jan 21, 2020):

yes right, i get that json response but i can't access to hash and then save into DB.
for example i have this global function that helps me to upload an image:
public function upload_img($file) {

    $ch = curl_init();
    $cfile = new CURLFile($file);

    $data = array("file"=>$cfile);

    $url = "http://cdn.ceebo.com/api/upload.php";
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    $response = curl_exec($ch);
    print_r($response['url']);
    if($response === true)
    {
        $output = $response['hash'];
    }
    else
    {
        $output = "error occured";
    }

  return $output;
}

Now i'd like to get only hash ID from $output, but is returns NULL

<!-- gh-comment-id:576712735 --> @vitalijalbu commented on GitHub (Jan 21, 2020): yes right, i get that json response but i can't access to hash and then save into DB. for example i have this global function that helps me to upload an image: public function upload_img($file) { $ch = curl_init(); $cfile = new CURLFile($file); $data = array("file"=>$cfile); $url = "http://cdn.ceebo.com/api/upload.php"; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS,$data); $response = curl_exec($ch); print_r($response['url']); if($response === true) { $output = $response['hash']; } else { $output = "error occured"; } return $output; } Now i'd like to get only hash ID from $output, but is returns NULL
Author
Owner

@geek-at commented on GitHub (Jan 21, 2020):

To get the response using the curl php extension you need to set returntransfer to true.

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

Also, because the answer from the server is a JSON object you'll have to parse it first before being able to use it as an array

Try this

$ch = curl_init();
$cfile = new CURLFile($file);

$data = array("file"=>$cfile);

$url = "http://cdn.ceebo.com/api/upload.php";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch),true);
var_dump($response]);
<!-- gh-comment-id:576720414 --> @geek-at commented on GitHub (Jan 21, 2020): To get the response using the curl php extension you need to set returntransfer to true. `curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);` Also, because the answer from the server is a JSON object you'll have to parse it first before being able to use it as an array Try this $ch = curl_init(); $cfile = new CURLFile($file); $data = array("file"=>$cfile); $url = "http://cdn.ceebo.com/api/upload.php"; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS,$data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = json_decode(curl_exec($ch),true); var_dump($response]);
Author
Owner

@vitalijalbu commented on GitHub (Jan 21, 2020):

thanks it's working :D

<!-- gh-comment-id:576734484 --> @vitalijalbu commented on GitHub (Jan 21, 2020): thanks it's working :D
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/pictshare#80
No description provided.