Hello and thanks in advance, now I'm trying to upload a image to a prestashop 1.7 via webservice and I'm unable to insert a image in a product. I don't know what fails, because I don't get any response of the webservice, even with the debug enabled (I get the xml responses of the rest of the files, but not the ones from curl).
The variable $idProduct is a value passed to the function and is defined.
My code is the following:
$url = PS_SHOP_PATH."api/images/products/".$idProduct;
$dir_path_to_save = 'img/import/';
$img_path = getFile($remoteImageURL, $dir_path_to_save);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '#'.$img_path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
echo print_r($response);
curl_close($ch);
The getFile function downloads the image to the server where is installed the prestashop and returns the path (returns a string with the real path where the image is stored, already tested).
I tried to make a form to upload the image (just for testing), but it returns "code 66 - unable to save this image". I don't know if this helps.
Thanks
UPDATE
A fellow programmer told me to use curl_file_create()
So I changed the $img_path declaration this way:
$img = curl_file_create($dir_path_to_save.'/'.basename($img_path));
Now everything works as intended.
Related
One of the pages of a website i'm working on should display information about a manga such as it's cover image.
I'm trying to display an image I got by making a curl request.
<?php
if(isset($_GET['info'])) {
$postedData = $_GET["info"]; //json object containing info about manga such as author/title etc.
$info = json_decode($postedData, true);
$mangacoverid = $info['im']; //id of the cover image that i'm getting from json object
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://cdn.mangaeden.com/mangasimg/" . $mangacoverid); //link of API i'm using and adding cover id to it
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$picture = curl_exec($ch);
curl_close($ch);
header('Content-type: image/jpeg');
echo $picture; //displays image in browser
}
?>
-Some 'mangacoverid' for testing purposes:
ff/ff94bb880357b6b811bccbbfd3356c5ec41fbb184291323f0ed6a86a.jpg
c1/c1b0173d8986681f23ecf5a69d26aa9dab4a04db4d40f99bed539198.jpg
0c/0cf6ebf78074e748ab2aeea3a0fcb9e0dd43040974c66e24fa46703f.jpg
5d/5dcfed2e033c2da62e7ac89367533ebbc344373c46a005e274a16785.png
18/18b1f0b13bccb594c6daf296c1f9b6dbd83783bb5ae63fe1716c9091.jpg
35/35bf6095212da882f5d2122fd547800ed993c58956ec39b5a2d52ad4.jpg
-While I am able to display the image in the page, the whole page background becomes black with the image in the middle of the page. (i.e. style="margin: 0px; background: #0e0e0e;>").
What I am trying to do is to insert the image in an HTML tag, so that I can place it somewhere else in the page.
I tried just putting the normal cdn link with 'mangacoverid' attached to it in an tag but the image provider doesn't allow hotlinking and it throws 403 error.
Any help really appreciated!
I tested your code and it seems to be working correctly when the cover ID is hard coded.
Could the issue be that the JSON passed via query param is not getting parsed correctly (due to URL encoding)?
Does it work correctly for you with a cover ID hard coded?
File image.php
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, sprintf('https://cdn.mangaeden.com/mangasimg/%s', $_GET['id']));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$picture = curl_exec($ch);
curl_close($ch);
header('Content-type: image/jpeg');
echo $picture;
And in your script that generates the page displaying the image:
// Assuming $info holds the decoded json for the manga
echo(sprintf('<img src="image.php?id=%s>', $info['im']);
Ok, I'll post the solution in case anyone ever bumps into this.
The code is the mostly the same as original: just edited a few lines as shown below.
-I created a file in the directory called 'tempcover.jpg'.
-I got the image using the function 'imagecreatefromstring($picture)'.
-I saved the image into the file with 'imagejpeg($img,'tempcover.jpg', 100)'.
By doing this I can just refer to the file in the HTML tag and display it the way I want to.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://cdn.mangaeden.com/mangasimg/" . $mangacoverid); //link of API i'm using and adding cover id to it
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$picture = curl_exec($ch);
curl_close($ch);
$img = imagecreatefromstring($picture);
imagejpeg($img,'tempcover.jpg', 100);
?>
<!--HTML-->
<img src="tempcover.jpg" alt="bruh" height="400px" width="300px">
When I try to add an image to a product I don't get any errors, but the image doesn't get added.
This is my code:
function addImage($idProduct)
{
$key = 'XXXXXXXXXXXXXXXXXXX';
$url = "http://192.168.1.81/api/images/products/".$idProduct;
$image_path = 'image2.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, $key);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '#'.$image_path.';type=image/jpg'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo '<h2>Image Added</h2>';
}
I also made a change on PSWebServiceLibrary.php, because Prestashop Web Service API keeps asking for authentication. This is the link where I got the code Prestashop Web Service API keeps asking for authentication .
This is the code I added:
$url .= '&ws_key=' . $this->key;
The problem is that the code to add an image was working before I made that change on PSWebServiceLibrary.php, and I don't know how to solve it.
I am using prestashop 1.6.1.5
Any help will be appreciated.
Greetings!
I think it's only a missing of "?" in your url.
If I follow your $url logic the result will be for product of ID 1234567 by example :
http://192.168.1.81/api/images/products/1234567&ws_key=ZOEJFD3429JD209AZJX0DJF20
So your server waits this url to hanlde ws_key as GET parameter :
http://192.168.1.81/api/images/products/1234567?ws_key=ZOEJFD3429JD209AZJX0DJF20
You need to add this "?" at the end of you URL like this :
$url = "http://192.168.1.81/api/images/products/".$idProduct."?";
Best Regards,
TGA
For me works like this:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => curl_file_create($image_path)));
I have been working with Jira API and have seen inconsistent results for my requests. Sometimes it works and sometimes it doesn't. Last week I was able to post attachments to issues just fine, but now an old problem occurred: the names of the attachments contain the whole path of the posted file, hence the attachments can't be opened. I use json representation to post files:
$array = array("file"=>"#filename");
json_encode($array);
...
This gets the file posted but the problem is when it's posted the file names in JIRA are like this:
/var/www/user/text.text
Needless to say it can't be opened in JIRA. I had this problem before, then it suddenly disappeared, now it occurred again. I don't really get it. By the way I am not using curl for this request even though it might be recommended in the documentation.
I realize this question is somewhat old but I had a similar problem. It seems Jira doesn't necessarily trim the filename as expected. I was able to fix it with the following. If you're using PHP >= 5.5.0:
$url = "http://example.com/jira/rest/api/2/issue/123456/attachments";
$headers = array("X-Atlassian-Token: nocheck");
$attachmentPath = "/full/path/to/file";
$filename = array_pop(explode('/', $attachmentPath));
$cfile = new CURLFile($attachmentPath);
$cfile->setPostFilename($filename);
$data = array('file'=>$cfile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
$ch_error = curl_error($ch);
if ($ch_error){
echo "cURL Error: $ch_error"; exit();
} else {
print_r($result);
}
For PHP <5.5.0 but > 5.2.10 (see this bug):
$data = array('file'=>"#{$attachmentPath};filename={$filename}");
Yes, I filed an issue on this at https://jira.atlassian.com/browse/JRA-30765
Adding attachments to JIRA by REST is sadly not as useful as it could be.
Interesting that the problem went away - perhaps you were running your script from a different location?
I am trying to retrieve users display picture through graph api and using cUrl to save it into the disk, but am unable succeed in it and getting this error when trying to check the mime type of the picture that I saved:
Notice: exif_imagetype(): Read error! in
//$userPpicture = $user_profile[picture];
//Create image instances
$url = "http://graph.facebook.com/{$userId}/picture?type=large";
$dpImage = 'temp/' . $userId . '_dpImage_' . rand().'.jpg';
echo $dpImage;
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data($url);
file_put_contents($dpImage, $returned_content);
echo "Type: " . exif_imagetype($dpImage);
for this updated code using curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); I am getting this error:
Warning: curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in /var/fog/apps/app12345/myapp.phpfogapp.com/start.php on line 178
If this action requires any server side configuration then i might not be able to do this as am using a shared cloud storage over phpfog.
Kindly help me with this.
Thankyou.
The graph url you are using of http://graph.facebook.com/4/picture?type=large returns a HTTP 302 redirect, not the actual user image. You would need to follow the redirect and download the image at that url which is a url that looks like this: http://profile.ak.fbcdn.net/hprofile-ak-snc4/49942_4_1525300_n.jpg
As OffBySome points out, you need to follow the 302 redirect served by graph.facebook.com to the final destination, which contains the actual image data.
The simplest way to do that in this case is to add another curl_setopt call with CURLOPT_FOLLOWLOCATION as true. i.e.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true)
Check out http://us3.php.net/manual/en/function.curl-setopt.php for more details.
I'm having a little trouble updating backgrounds via Twitter's API.
$target_url = "http://www.google.com/logos/11th_birthday.gif";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
$content = $to->OAuthRequest('http://twitter.com/account/update_profile_background_image.xml', array('profile_background_image_url' => $html), 'POST');
When I try to pull the raw data via cURL or file_get_contents, I get this...
Expectation Failed The expectation given in the Expect request-header
field could not be met by this server.
The client sent
Expect: 100-continue but we only allow the 100-continue expectation.
OK, you can't direct Twitter to a URL, it won't accept that. Looking around a bit I've found that the best way is to download the image to the local server and then pass that over to Twitter almost like a form upload.
Try the following code, and let me know what you get.
// The URL from an external (or internal) server we want to grab
$url = 'http://www.google.com/logos/11th_birthday.gif';
// We need to grab the file name of this, unless you want to create your own
$filename = basename($url);
// This is where we'll be saving our new file to. Replace LOCALPATH with the path you would like to save the file to, i.e. www/home/content/my_directory/
$newfilename = 'LOCALPATH' . $filename;
// Copy it over, PHP will handle the overheads.
copy($url, $newfilename);
// Now it's OAuth time... fingers crossed!
$content = $to->OAuthRequest('http://twitter.com/account/update_profile_background_image.xml', array('profile_background_image_url' => $newfilename), 'POST');
// Echo something so you know it went through
print "done";
Well, given the error message, it sounds like you should load the URL's contents yourself, and post the data directly. Have you tried that?