Daniel Benzie
Daniel Benzie

Reputation: 479

uploading photo via facebook API - pathname issues

I'm currently having problems uploading an image via the facebook API - I am not returned any errors via the API and at this time I am thinking it is likely an issue with the file path I am passing in is incorrect.

The image is stored on our server after the user has used the app and I would then like to post it to facebook.

My code is below:

after running I receive no response from the API whereas if I for instance deliberately changed the oauth_token I'd receive an error (so I know the data is definitely posting)

I'm also printing the args array so I can see exactly what I'm posting:

here is the response.

If I navigate to the URL after the @ sign it is broken even if I remove the @C:\Domains and replace with www. I have tried passing in the URL that a user could actually navigate to if they were to know the filename

e.g https://www.example.co.uk/competition/fb/photos/collagetest.jpg

^ this is a valid link but when passing in the image is still not actually uploaded.

Any help would be massively appreciated!

Array ( [message] => Photo from ********[image] => @C:\Domains\******.co.uk\wwwroot\v2\competition\fb\photos\collagetest.jpg )

//upload photo
            $file= './photos/'.basename('collagetest.jpg');
            $args = array(
               'message' => 'Photo from friendbooth',
            );
            $args['image'] = '@' . realpath($file);
            $ch = curl_init();
            $url = 'https://graph.facebook.com/photos?access_token='.$_SESSION['oauth_token'];
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
            $data = curl_exec($ch);
            print_r($args);
            //returns the photo id
            print_r(json_decode($data,true));

            ?>
            <img src="collagetest.jpg" />
            <?

Upvotes: 0

Views: 219

Answers (2)

Daniel Benzie
Daniel Benzie

Reputation: 479

turns out it was nothing to do with the file path in the end!

adding

            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

completely resolved the issue! Hope this helps someone else in future. if posting to HTTPS urls try adding the above in if you aren't getting ANY response (error messages etc mean you are getting a response)

Upvotes: 0

C3roe
C3roe

Reputation: 96325

If I navigate to the URL after the @ sign it is broken

What do you mean by „broken”?

Is that file system path readable from within your PHP script? (Please check, don’t guess.)

even if I remove the @C:\Domains and replace with www. I have tried passing in the URL

Giving a URL in this place is nonsense – a file system path is expected here. (Are you aware of the difference between the two?)

Upvotes: 1

Related Questions