I have a cron file
that runs to post images to Instagram, once the image get posted, the code updates the MySQL table as successful. some times, if the logging to Instagram was not successful (or) IP blocked then a server response shows ("status":"fail")
. how to use (if) to read the http response then update MySQL as fail?
PHP Version: 5.6.35
Server http response:
{"message": "checkpoint_required", "checkpoint_url": "https://i.instagram.com/challenge/8633340132/qz34BIU1MQ/", "lock": true, "status": "fail", "error_type": "checkpoint_challenge_required"}
Current posting code:
<?php
require_once("helper/functions.php");
require_once("cron_insta_include.php");
$result_outmsg = $database->query("SELECT * FROM insta_sent");
while($row_outmsg = $database->fetch_array($result_outmsg))
{
if($row_outmsg["report"] == "Scheduled")
{
$res_ch = $database->query("SELECT * FROM insta_ch WHERE serial='" . $row_outmsg["fromch"] . "'");
$row_ch = $database->fetch_array($res_ch);
if(($row_ch["status"] != "Blocked") && ($row_ch != false))
{
if((time() - $row_ch["last_used"]) > 7200)
{
try
{
//Insta Sending
$cap = "." . "
" . $row_outmsg["body"] . "
" . "." . "
" . $row_outmsg["hash"] . "
" . "." . "
" . $row_outmsg["tonum"] . "
" . "." . "
" . time();
$obj = new InstagramUpload();
$obj->Login($row_ch["phone"], $row_ch["ch_id"]);
$obj->UploadPhoto($row_outmsg["img_lnk"], $cap);
sleep(1);
$database->query("UPDATE insta_sent SET report='Sent' WHERE serial='" . $row_outmsg["serial"] . "'");
}
catch(Exception $e)
{
$database->query("UPDATE insta_sent SET report='Failed' WHERE serial='" . $row_outmsg["serial"] . "'");
}
$sql_13 = "UPDATE insta_ch SET last_used='" . time() . "' WHERE serial='" . $row_outmsg["fromch"] . "'";
$database->query($sql_13);
}
}
else
{
//$database->query("UPDATE insta_sent SET report='Not Sent' WHERE serial='" . $row_outmsg["serial"] . "'");
}
}
}
?>
You can either use curl directly to get http responses or you can use library like guzzle http. Please refer guzzle http doc : http://docs.guzzlephp.org/en/stable/
Sample curl post request is like this :
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, "your_data_array");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: "length_of_your_data_array"')
);
$result = curl_exec($ch);
$obj = new InstagramUpload();
$output = $obj->Login($row_ch["phone"], $row_ch["ch_id"]);
$resp = json_decode($output, true);
if($resp['status'] !='fail') {
$obj->UploadPhoto($row_outmsg["img_lnk"], $cap);
$database->query("UPDATE insta_sent SET report='Sent' WHERE serial='" . $row_outmsg["serial"] . "'");
}
As marv255 pointed out, the used class just prints the result and does not return it. Fortunately, there is also the appropriate PHP function for such scenarios. Just use output buffering.
ob_start();
$instance = new InstagramUpload();
$instance->Login($phone, $chid);
$instance->UploadPhoto($image, $caption);
$response = ob_get_contents();
ob_end_clean();
$result = json_decode($response);
if (!$result) {
// json string could not be decoded
}
if (property_exists($result, 'status') && $result->status == 'fail') {
// negative response
} else {
// positive response
}
The $response
variable now contains the string returned from the API. In this case you can parse the received string and if fail
occurs, you know that the request has failed and you can save it in your database.
I have updated the cron file to be as follows and still it can not update the MySQL upone failer
<?php
require_once("helper/functions.php");
require_once("cron_insta_include.php");
$result_outmsg = $database->query("SELECT * FROM insta_sent");
while($row_outmsg = $database->fetch_array($result_outmsg))
{
if($row_outmsg["report"] == "Scheduled")
{
$res_ch = $database->query("SELECT * FROM insta_ch WHERE serial='" . $row_outmsg["fromch"] . "'");
$row_ch = $database->fetch_array($res_ch);
//if(($row_ch["status"] != "Blocked") && ($row_ch != false))
if($row_ch["status"] == "Active")
{
if((time() - $row_ch["last_used"]) > 10800)
{
try
{
//Insta Sending
$cap = "." . "
" . $row_outmsg["body"] . "
" . "." . "
" . $row_outmsg["hash"] . "
" . "." . "
" . $row_outmsg["tonum"] . "
" . "." . "
" . time();
$obj = new InstagramUpload();
$obj->Login($row_ch["phone"], $row_ch["ch_id"]);
sleep(2);
$obj->UploadPhoto($row_outmsg["img_lnk"], $cap);
sleep(2);
$database->query("UPDATE insta_sent SET report='Sent' WHERE serial='" . $row_outmsg["serial"] . "'");
}
catch(Exception $e)
{
$database->query("UPDATE insta_sent SET report='Failed' WHERE serial='" . $row_outmsg["serial"] . "'");
}
$sql_13 = "UPDATE insta_ch SET last_used='" . time() . "' WHERE serial='" . $row_outmsg["fromch"] . "'";
$database->query($sql_13);
}
}
else
{
$database->query("UPDATE insta_sent SET report='Not Sent' WHERE serial='" . $row_outmsg["serial"] . "'");
}
}
}
?>