hi guys i have this script but obviously im not using foreach right i was wondering is there anyway i could combine these two requests to works as one
$url = "http://api.website.com/1.0/country?source=ballsche&programid=5380&campaignid=100000";
$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);
$array = json_decode($rawdata,true);
foreach($array as $obj) {
$country = $obj['country'];
$countrycode = $obj['countryCode'];
}
foreach($countrycode as $did) {
$wgurl = "http://api.website.com/1.0/city?source=ballsche&programid=5380&campaignid=100000&country-code=$did";
$wgch = curl_init();
$wgtimeout = 0;
curl_setopt($wgch, CURLOPT_URL, $wgurl);
curl_setopt($wgch, CURLOPT_HEADER, 0);
curl_setopt($wgch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($wgch, CURLOPT_CONNECTTIMEOUT, $wgtimeout);
$wgrawdata = curl_exec($wgch);
curl_close($wgch);
$wgarray = json_decode($wgrawdata,true);
}
foreach($wgarray as $wgobj) {
$city = $wgobj['city'];
$citycode = $wgobj['cityCode'];
if($city){
$sql = "INSERT INTO `pt_city` (city, citycode) VALUES ('$city', '$citycode')";
database_queryModify($sql,$result);
}else{
echo "dint work";
}
}
there must be an easy way to do this im guesing creating another array from the data but i cant quite get it right i keep getting errors ive tried this and a few other things my problem is i need to cycle trough county codes and make requests from the code there a 150 requests i need to cycle trough to get all the city information and for each request i need to decode the json thats coming back and insert it into my city table
just put foreach inside each other like this
$url = "http://api.website.com/1.0/country?source=ballsche&programid=5380&campaignid=100000";
$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);
$array = json_decode($rawdata,true);
foreach($array as $obj) {
$country = $obj['country'];
$countrycode = $obj['countryCode'];
foreach($countrycode as $did) {
$wgurl = "http://api.website.com/1.0/city?source=ballsche&programid=5380&campaignid=100000&country-code=$did";
$wgch = curl_init();
$wgtimeout = 0;
curl_setopt($wgch, CURLOPT_URL, $wgurl);
curl_setopt($wgch, CURLOPT_HEADER, 0);
curl_setopt($wgch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($wgch, CURLOPT_CONNECTTIMEOUT, $wgtimeout);
$wgrawdata = curl_exec($wgch);
curl_close($wgch);
$wgarray = json_decode($wgrawdata,true);
foreach($wgarray as $wgobj) {
$city = $wgobj['city'];
$citycode = $wgobj['cityCode'];
if($city){
$sql = "INSERT INTO `pt_city` (city, citycode) VALUES ('$city', '$citycode')";
database_queryModify($sql,$result);
} else {
echo "dint work";
}
}
}
}
Related
I have a very strange problem. I am trying to return an array from a JSON GET request using the Skyscanner API.
The Skyscanner URL I am generating and using in the cURL is: http://partners.api.skyscanner.net/apiservices/browseroutes/v1.0/DE/EUR/de-DE/HAM/PT/2016-12?apiKey=API_KEY
It returns 3 quotes but when I am using a JSON editor like http://www.jsoneditoronline.org/ it gives me 9 quotes!!!
Why does it show less quotes in my solution? And how do I get all quotes???
<?php
$cities = array("HAM");
$countries = array("PT");
$url_api = "http://partners.api.skyscanner.net/apiservices/browseroutes/v1.0/DE/EUR/de-DE/";
$api_key = "API_KEY_HERE";
$next_month = date('m')+1;
if ($next_month=="13") {
$this_year = date('Y')+1;
$next_month = "01";
} else {
$this_year = date('Y');
}
foreach($cities as $city){
foreach($countries as $country){
$url = $url_api.$city."/".$country."/".$this_year."-".$next_month."?apiKey=".$api_key;
$ip = "218.255.245.210";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'X_FORWARDED_FOR: '.$ip));
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
$data = curl_exec($ch);
$response = json_decode($data);
echo $url."<br/>";
$quotes = $response->Quotes;
foreach($quotes as $quote){
echo $quote->QuoteId.". ";
echo $quote->Direct."<br/>";
}
}
}
?>
I wanted to make an inline bot! and when i do this:
function sendResponse($url, $data){
$ch = curl_init();
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('inline_query_id' => $data['inline_query_id'], 'results' => json_encode($data['results'])));
$output = curl_exec($ch);
return $output;
}
It wont work, the error (with or without the header): {"ok":false,"error_code":400,"description":"[Error]: Bad request: Field \"message_text\" must be of type String"}
but when I do it like this:
function sendResponse($url, $data){
$ch = curl_init();
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_URL, $url.'?inline_query_id='.rawurlencode($data['inline_query_id']).'&results='.rawurlencode(json_encode($data['results'])));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $q);
$output = curl_exec($ch);
return $output;
}
It works ! the problem is the second method request URI will be too large so I cannot use it!
Any way I can send these data is okay with me! thanks!
and the code for making $data is here:
$result = connectWebsite(SITE_SEARCH_URL, urlencode($update['inline_query']['query']));
$result = json_decode($result);
$output = array();
$output['inline_query_id'] = $update['inline_query']['id'];
$i = 0;
foreach($result as $post){
$data = array();
$data['type'] = 'article';
$data['id'] = strval($post->ID);
$data['title'] = '('.$post->atypes.') '.$post->title;
if(strlen($post->content) > 2100)
$tmp = substr($post->content, 0, 2096).'...';
$data['message_text'] = '<b>'.$post->title.'</b>'.ucwords($post->genre, ',').$tmp;
$data['parse_mode'] = 'HTML';
if(strlen($post->content) > 200)
$tmp = substr($post->content, 0, 196).'...';
//$data['description'] = ucwords($post->genre, ',').' | '.$tmp;
$output['results'][$i] = $data;
$i++;
if($i == MAX_RESULTS)
break;
}
sendResponse(API_URL.'answerInlineQuery', $output);
It might help someone so I'll answer it myself.
the problem was the UTF-8 encoding
I replaced substr with mb_substr
besides at the first line I'v added this: mb_internal_encoding("UTF-8")
and ... the problem was solved. now I can send my inline query results (or any other command) without the URL length problem
Thanks everyone for your help
When i am Decoding using commented "$jsonString" String it is working very well.
But after using curl it is not working, showing Null.
Please Help Me in this.
if (isset($_POST['dkno'])) {
$dcktNo = $_POST['dkno'];
$url = 'http://ExampleStatus.php?dkno=' . $dcktNo;
$myvars = '';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonString = curl_exec($ch);
// $jsonString = '[{"branchname":"BHUBNESHWAR","consignee":"ICICI BANK LTD","currentstatus":"Delivered by : BHUBNESHWAR On - 25/07/2015 01:00","dlyflag":"Y","PODuploaded":"Not Uploaded"}]';
if ($jsonString != '') {
$json = str_replace(array('[', ']'), '', $jsonString);
echo $json;
$obj = json_decode($json);
if (is_null($obj)) {
die("<br/>Invalid JSON, don't need to keep on working on it");
} else {
$podStatus = $obj->PODuploaded;
}
}
}
}
After curl I used following concept to get only JSON data from HTML Page.
1) fetchData.php
$url = 'http://DocketStatusApp.aspx?dkno=' . $dcktNo;
$myvars = '';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonString = curl_exec($ch);
// now get only value
$dom = new DOMDocument();
$dom->loadHTML($jsonString);
$thediv = $dom->getElementById('Label1');
echo $thediv->textContent;
2) JSONprocess.php
if (isset($_POST['dkno'])) {
$dcktNo = $_POST['dkno'];
ob_start(); // begin collecting output
include_once 'fetchData.php';
$result = ob_get_clean(); // Completed collecting output
// Now it will show & take only JSON Data from Div Tag
$json = str_replace(array('[', ']'), '', $result);
$obj = json_decode($json);
if (is_null($obj)) {
die("<br/>Invalid JSON, don't need to keep on working on it");
} else {
$podStatus = $obj->PODuploaded;
}
}
<?php
ini_set('display_errors',1);
$url = 'www.google.com.my';
$header = true;
$returntransfer = true;
$connecttimeout = 3;
$timeout = 60;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $returntransfer);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connecttimeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$execute = curl_exec($ch);
$info = curl_getinfo($ch);
header('Content-Type: text/plain');
echo $execute;
echo "\n\ncurl_getinfo() said:\n", str_repeat('-', 31 + strlen($url)), "\n";
foreach ($info as $label => $value)
{
printf("%-30s %s\n", $label, $value);
}
echo str_repeat('-', 31 + strlen($url));
?>
Here is my questions:
(1) I want to test the web services using PHP curl. Am I missing something based from the above code?
(2) If I have 2 URLs, should I use curl_setopt or curl_multi_init?
I really hope that someone will answer my questions.
It's a simultaneous requests:
$ch_1 = curl_init('http://url.one.com/');
$ch_2 = curl_init('http://url.two.com/');
curl_setopt($ch_1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_2, CURLOPT_RETURNTRANSFER, true);
// build the multi-curl handle, adding both $ch
$mh = curl_multi_init();
curl_multi_add_handle($mh, $ch_1);
curl_multi_add_handle($mh, $ch_2);
// execute all queries simultaneously, and continue when all are complete
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running);
// all of our requests are done, we can now access the results
$response_1 = curl_multi_getcontent($ch_1);
$response_2 = curl_multi_getcontent($ch_2);
echo "$response_1 $response_2"; // same output as first example
Is there still no way to invite all the group members to an group event? I am able to create the group event via the api, but members are not invited to the new event. If i create the event via the facebook website i have the option to select "invite all group members". I can't seem to find any way to reproduce that functionality via the api.
Since there was no way to do this directly via the graph api i ended up making my own php w/mysql solution. Basically i make a table of all my group's members, then i check if they have been invited to all my group's events. the good thing about this forced method is that if a new members joins after an event is created they will be invited to all future events.
facebook does not tell you how many members you can invite from the api per time period and not get flagged for spam
code to dump group members:
function UpdateFacebookMemberDB($verbatim = FALSE)
{
global $access_token, $groupID;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$groupID.'/members?access_token='.$access_token);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close ($ch);
$decoded = json_decode($return, true);
$decoded = $decoded['data'];
Sql_Connect();
if(!Sql_Query("DELETE FROM `Facebook_User`;"))
{
echo "Could not empty the `Facebook_User` table.<br>\n";
return FALSE;
}
foreach ($decoded as $value)
{
$query="INSERT INTO `Facebook_User` (`Name`, `FID`) VALUES ('".Sql_CleanInput($value['name'])."', '".Sql_CleanInput($value['id'])."');";
if(Sql_Query($query))
{
if($verbatim)
echo $value['name']." was added to the database.<br>";
}
else if($verbatim)
echo $value['name']." was <b>NOT</b> added to the database.<br>";
}
if(!Sql_Query("DELETE FROM `Facebook_Event`;"))
{
echo "Could not empty the `Facebook_Event` table.<br>\n";
return FALSE;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$groupID.'/events?fields=start_time&since=now&access_token='.$access_token);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close ($ch);
$decoded = json_decode($return, true);
$decoded = $decoded['data'];
foreach ($decoded as $value)
{
$eid = $value['id'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$eid.'/invited?access_token='.$access_token);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close ($ch);
$decod = json_decode($return, true);
$decod = $decod['data'];
foreach ($decod as $val)
{
$query="INSERT INTO `Facebook_Event` (`EventID`, `UserID`) VALUES ('".$eid."', '".$val['id']."');";
if(Sql_Query($query))
{
if($verbatim)
echo $val['name']." was added to the database.<br>";
}
else if($verbatim)
echo $val['name']." was <b>NOT</b> added to the database.<br>";
}
}
Sql_Disconnect();
}
code to check/invite members:
function InviteClubToEvent)
{
global $access_token, $groupID;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$groupID.'/events?fields=start_time&since=now&access_token='.$access_token);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close ($ch);
$decoded = json_decode($return, true);
$decoded = $decoded['data'];
$invite=0;
for($x=count($decoded)-1;$x>=0;$x--)
{
$eventid = $decoded[$x]['id'];
Sql_Connect();
$query="SELECT * from `Facebook_User` ORDER BY `FID` ASC;";
$qresult=Sql_Query($query);
$data = array( 'access_token' => $access_token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
for($i=0;$i<Sql_Num_Rows($qresult);$i++)
{
$subquery="SELECT * from Facebook_Event WHERE `UserID` ='".Sql_Result($qresult,$i,"FID")."' AND `EventID` ='".$eventid."'LIMIT 1;";
if(Sql_Num_Rows(Sql_Query($subquery))==0)
{
$invite++;
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$eventid.'/invited/'.Sql_Result($qresult,$i,"FID"));
$return = curl_exec($ch);
if($return == "true")
{
echo "$invite) ".Sql_Result($qresult,$i,"Name") . " has been invited to $eventid!<br>";
Sql_Query("INSERT INTO `sdbmwcca_main`.`Facebook_Event` (`EventID`, `UserID`) VALUES ('".$eventid."', '".Sql_Result($qresult,$i,"FID")."');");
} else
{
echo "<hr><b>$invite) ".Sql_Result($qresult,$i,"Name") . " was not invited because: " . $return . "</b><hr>";
curl_close($ch);
Sql_Disconnect();
die();
}
}
if($invite > ##some spam limit##)
{
$x = 0;
break;
}
}
curl_close ($ch);
Sql_Disconnect();
}
}