Open pk-pro opened 8 years ago
+1
function SendRequest($url, $post, $post_data, $user_agent, $cookies) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://i.instagram.com/api/v1/'.$url);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
if($post) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
}
if ((version_compare(PHP_VERSION, '5.5') >= 0)) {
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
}
if($cookies) {
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__). '/cookies.txt');
} else {
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__). '/cookies.txt');
}
$response = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array($http, $response);
}
function GetPostData($filename)
{
$path = getcwd() .'/uploads/' . $filename;
if (!$path) {
echo "The image doesn't exist " . $path;
} else {
$withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename);
$convertImageToJpg = $this->ConvertImage($path, './uploads/' . 'converted_' . $withoutExt . '.jpg', 100);
if ($convertImageToJpg) {
$convertedPath = getcwd() .'/uploads/' . 'converted_' . $withoutExt . '.jpg';
$instagramPath = getcwd() .'/uploads/' . 'instagram_' . $withoutExt . '.jpg';
$this->SquareImage($convertedPath, $instagramPath);
$post_data = array('device_timestamp' => time());
if ((version_compare(PHP_VERSION, '5.5') >= 0)) {
$post_data['photo'] = new CURLFile(realpath($instagramPath));
} else {
$aPost['photo'] = "@".realpath($instagramPath);
}
array_push($this->unlinkPaths, $convertedPath, $path, $instagramPath);
return $post_data;
}
}
}
Issues with php version, replace the GetPostData and SendRequest method
@Shypes submit a pull request if this fixes the issue.
it work on a php page, but inside my website, i use a controller to execute the action, and got an error:
array(2)
[0]int200
[1]string(84)"{"status":"ok","errors":{"error":["Sorry, there was a problem with your request."]}}"
the action is call by an AJAX, maybe this is the cause
@ibasaw this code was originally designed for desktop usage, some changes need to be made to make it acceptable for web usage, specifically the way the cookie is grabbed. IF you look at SendRequest, the cookiejar is just saving the request to cookies.txt, this is a bit insecure and will disallow multiple request at once. It's possible the cookie is failing to be located on your page.
change 'photo' => '@'.$filename to 'photo' => new CURLFile($filename) php 5.6+
so, is there a solution for that error please ?
error disappeared when I changed hosting
@pk-pro You mean that it can't be done from localhost ?
I wrote a c# version of this that is more secure and faster
ashrafhesham-92 I do not know how about localhost, I did it in web hosting. Maybe it is due to IP adress...
mgp25 has a similar script that I use yet I also encounter the same media_needs_reupload / staged_position_not_found when I upload videos. I been posting on mgp25's GitHub trying to find a solution yet to find a solution. Some people suggest downgrade to 5.5.3 but that did not fix fyi. https://github.com/mgp25/Instagram-API/issues/191 https://github.com/mgp25/Instagram-API/issues/185
The answer of kiwina is the solution of the error "staged_position_not_found" for who use a PHP version >= 5.6+
change 'photo' => '@'.$filename to 'photo' => new CURLFile($filename) php 5.6+
My code is with 'photo' => new CURLFile($filename) php 7.0 but showing this error, any ideas?
array(4) { ["message"]=> string(20) "media_needs_reupload" ["media"]=> array(1) { ["upload_id"]=> string(10) "XXXXXXX" } ["error_title"]=> string(34) "staged_position_not_found extra:{}" ["status"]=> string(2) "ok" }
@andreescocard Same here. Instagram could change their API I guess. Have you solved this issue?
Ainda nada né?!
| ( | [message] => media_needs_reupload | [media] => Array | ( | [upload_id] => 1544661292 | ) | | [error_title] => staged_position_not_found extra:{} | [status] => ok | )
:(
insta 😈 won!!!
Have you solved it somehow?
Hello! Thanks for the work! How this FIX?