Curl request method inside foreach loop got a error - php

I'm getting an error "Undefined offset: 1" when i use foreach (option 1) instead of duplicate code (option 2). With foreach, i'm using an array only with 2 strings to test. The error is on line $html_tt = get($direct_link_tt[1]);.
Should i do something different in get method?
$cookie_path = dirname(__FILE__).'/cookie.txt';
function login($user, $pass, $account) {
$URL = 'https://example.com/index.php';
global $cookie_path;
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'url_login='.$user.'&url_password='.$pass.'&url_account='.$account);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
function get($url) {
global $cookie_path;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
function main() {
$user = 'XXXXXX';
$pass = 'XXXXXX';
$account = 'XXXXX';
$all_tt = getVals();
$dataLogin = login($user, $pass, $account);
preg_match('/"PHPSESSID", "(.*)"/U', $dataLogin, $phpSessionEV);
preg_match('/uniqid=([0-9]*)&/U', $dataLogin, $uniqid);
//option 1 start
foreach ($all_tt as $tt){
$url_bef_ticket = "https://example.com/ajax_widget.php?session_id=".$phpSessionEV[1]."&uniqid=".$uniqid[1]."&lng=PO&theme={C9F11D78-448E-4EAC-A766-C904ED98458E}&widgetname=InternalFilteredQuery.InternalFilteredQueryWidget&widgetid=Grid_78A562B7_B6EA_45BF_9223_A2D42C74851F&WidgetParams=action%3DgetContent%26blockGUID%3D%257BC99ECD05-3D48-4C62-ABF0-66292053AED6%257D%26eventName%3DRequestFullfimentItem%26filterGUID%3D%257B096C2D42-0DF7-4A54-9978-D13E59BAB8ED%257D%26internalGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26title%3DPedidos%2Bde%2BServi%25C3%25A7o%26maxLines%3D5%26queryGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26reportGUID%3D%257BC910B0A9-D202-42D4-8E75-CC48FC3EA1D8%257D%26searchText%3D".$tt."%26searchGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26themeGUID%3D%257BC9F11D78-448E-4EAC-A766-C904ED98458E%257D";
$html_bef_ticket = get($url_bef_ticket);
preg_match('/<a class="aImg" href="(.*)"/U', $html_bef_ticket, $direct_link_tt);
$html_tt = get($direct_link_tt[1]);
print_r($html_tt);
}
//option 1 end
//option 2 start
$url_bef_t = "https://example.com/ajax_widget.php?session_id=".$phpSessionEV[1]."&uniqid=".$uniqid[1]."&lng=PO&theme={C9F11D78-448E-4EAC-A766-C904ED98458E}&widgetname=InternalFilteredQuery.InternalFilteredQueryWidget&widgetid=Grid_78A562B7_B6EA_45BF_9223_A2D42C74851F&WidgetParams=action%3DgetContent%26blockGUID%3D%257BC99ECD05-3D48-4C62-ABF0-66292053AED6%257D%26eventName%3DRequestFullfimentItem%26filterGUID%3D%257B096C2D42-0DF7-4A54-9978-D13E59BAB8ED%257D%26internalGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26title%3DPedidos%2Bde%2BServi%25C3%25A7o%26maxLines%3D5%26queryGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26reportGUID%3D%257BC910B0A9-D202-42D4-8E75-CC48FC3EA1D8%257D%26searchText%3DS1234567%26searchGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26themeGUID%3D%257BC9F11D78-448E-4EAC-A766-C904ED98458E%257D";
$html_bef_ticket = get($url_bef_t);
preg_match('/<a class="aImg" href="(.*)"/U', $html_bef_ticket, $direct_link_tt);
$html_tt = get($direct_link_tt[1]);
print_r($html_tt);
$url_bef_t = "https://example.com/ajax_widget.php?session_id=".$phpSessionEV[1]."&uniqid=".$uniqid[1]."&lng=PO&theme={C9F11D78-448E-4EAC-A766-C904ED98458E}&widgetname=InternalFilteredQuery.InternalFilteredQueryWidget&widgetid=Grid_78A562B7_B6EA_45BF_9223_A2D42C74851F&WidgetParams=action%3DgetContent%26blockGUID%3D%257BC99ECD05-3D48-4C62-ABF0-66292053AED6%257D%26eventName%3DRequestFullfimentItem%26filterGUID%3D%257B096C2D42-0DF7-4A54-9978-D13E59BAB8ED%257D%26internalGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26title%3DPedidos%2Bde%2BServi%25C3%25A7o%26maxLines%3D5%26queryGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26reportGUID%3D%257BC910B0A9-D202-42D4-8E75-CC48FC3EA1D8%257D%26searchText%3DS7654321%26searchGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26themeGUID%3D%257BC9F11D78-448E-4EAC-A766-C904ED98458E%257D";
$html_bef_ticket = get($url_bef_t);
preg_match('/<a class="aImg" href="(.*)"/U', $html_bef_ticket, $direct_link_tt);
$html_tt = get($direct_link_tt[1]);
print_r($html_tt);
//option 2 end
}
main();
What i'm doing wrong?

I think you need to initialize both curl request with two different variables.
function login($user, $pass, $account) {
$URL = 'https://example.com/index.php';
global $cookie_path;
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'url_login='.$user.'&url_password='.$pass.'&url_account='.$account);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
function get($url) {
global $cookie_path;
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, $url);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch2, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_path);
$html = curl_exec($ch2);
curl_close($ch2);
return $html;
}
I hope this helps you.

Related

Why is the return access token not valid?

This code not saving access token after verifying that this access token is valid or not instead of that it returns i=1. Eight hours ago this script work fine but it suddenly stop working.
<?php
function get_json($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
$data = curl_exec($ch);
curl_close($ch);
return json_decode($data);
}
function get_html($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$token2 = $_GET["user"];
session_start();
if(isset($token2)){
if(preg_match("'access_token=(.*?)&expires_in='", $token2, $matches)){
$token = $matches[1];
}else{
$token = $token2;}
$exe = json_decode(get_html("https://graph.facebook.com/app?access_token=".$token ))->id;
$extend = get_html("https://graph.facebook.com/me/permissions?access_token=" . $token);
if($extend == true){
$pos = strpos($extend, "publish_actions");
if ($pos == true) {
$_SESSION['token'] = $token;
$ch = curl_init('http://my site/saver.php');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "token=".$token);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_exec ($ch);
curl_close ($ch);
header('Location:index.php');
}
else {
header('Location:index.php?i=1');}
//access token is not valid
}else{
header('Location:index.php?i=4');}
}else{
header('Location:index.php?i=2');}
?>

Trying to keep session with curl login in twitter

I'm trying to keep the session along different requests with curl when login into twitter because at some point it logs out from Twitter
$sTarget = "https://twitter.com";
curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie". $usuario ."_tweet.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, $usuario ."_tweet.txt");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_REFERER, $sTarget);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
$html = curl_exec($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($c);
}
preg_match('<input name="authenticity_token" type="hidden" value="([a-zA-Z0-9]*)">', $html, $match);
$authenticity_token = $match[1];
if ($authenticity_token == "");
{
preg_match('<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token">', $html, $matchprima);
$authenticity_token = $matchprima[1];
}
$username = $usuario;
$password = "*******";
$sPost = "session[username_or_email]=$username&session[password]=$password&return_to_ssl=true&scribe_log=&redirect_after_login=%2F&authenticity_token=$authenticity_token";
$sTarget = "https://twitter.com/sessions";
curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
# display server response
$htmldos = curl_exec($ch);
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $htmldos, $matches);
$cookies = array();
foreach($matches[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
var_dump($cookies);
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
But when doing a var_dump in cookies to see if the cookies values are the same, under ["_twitter_sess"] I get different strings in the 2 requests. Shouldn't I get the same string if the session is kept between requests? If so, how do I do that?
This code works for logging in Twitter, and keeping session as for doing other things such after it as sending tweets. I've been using it for 6 months without problem. You need simple_html_dom.php from PHP Simple HTML DOM Parser for it to work
<?php
include_once('simple_html_dom.php');
global $usuario;
$usuario = "universohumor2"; //your twitter account username
$ch = curl_init();
/************************************** COMIENZAN LAS LLAMADAS A FUNCIONES *************************************/
/***************************************************************************************************************/
$vector_retorno = logueo_twitter($ch);
/******************************************************************************************************************/
function logueo_twitter($ch)
{
//$ch = curl_init();
global $usuario;
$sTarget = "https://twitter.com";
curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie". $usuario ."_tweet.txt");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_REFERER, $sTarget);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
$html = curl_exec($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($c);
}
preg_match('<input name="authenticity_token" type="hidden" value="([a-zA-Z0-9]*)">', $html, $match);
$authenticity_token = $match[1];
if ($authenticity_token == "");
{
preg_match('<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token">', $html, $matchprima);
$authenticity_token = $matchprima[1];
}
$username = $usuario;
$password = "password"; // your twitter account password
$sPost = "session[username_or_email]=$username&session[password]=$password&return_to_ssl=true&scribe_log=&redirect_after_login=%2F&authenticity_token=$authenticity_token";
$sTarget = "https://twitter.com/sessions";
curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
# display server response
$htmldos = curl_exec($ch);
//echo $htmldos;
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
$vector["sPost"] = $sPost;
$vector["authenticity_token"] = $authenticity_token;
return $vector;
}
?>

Send POST with cURL postfields to iframe

I am trying to send POST data using CURL to an url. This URL is called by an iframe, which is in my application.
<iframe frameborder="0" width="950" height="1000" src="/tools/costs"></iframe>
public function costs()
{
$url = "http://someurl.com/tools/costs";
if($_POST)
{
$fields_str = http_build_query($_POST);
$query = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D',$fields_str);
$realstring = str_replace("%5D", "", str_replace("%5B", "", $query));
// $realstring echo:
var_carrier=3&var_carrier=5&var_carrier=6&var_service=Demurrage+%2F+Storage&var_service=Detention&var_pod=Ancona&var_pod=Antalya&var_pod=Antwerpen
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$realstring);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
}
else
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
$fields_str = "";
}
echo $response;
}
but the $realstring variable is empty when i send it by cURL POSTFIELDS
I have no idea wy!

curl is not collecting all the cookies from external site

for some reason , its not collecting all the cookies, its not collecting the password hash or the member id , im not sure why its not setting those since its getting the others, am i doing somthing wrong with my coding, this is my first time using curl
this is the information in the cookie.txt file
<?php
//init curl
function curl_file_get_contents($url){
$username = 'user#hotmail.com';
$password = 'mypass';
$loginUrl = 'http://forums.zybez.net/index.php?app=curseauth&module=global&section=login';
//init curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user=' . $username . '&pass=' . $password);
$store = curl_exec($ch);
curl_setopt($ch, CURLOPT_REFERER, 'http://forums.zybez.net/runescape-2007-prices/282-law+rune');
$content = curl_exec($ch);
curl_close($ch);
file_put_contents('~/download.zip', $content);
$curl = curl_init();
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_REFERER, $loginUrl);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
$contents = curl_exec($curl);
return $contents;
curl_close($curl);
}
function get_input_tags($html){
$post_data = array();
// a new dom object
$dom = new DomDocument;
//load the html into the object
$dom->loadHTML($html);
//discard white space
$dom->preserveWhiteSpace = false;
//all input tags as a list
$input_tags = $dom->getElementsByTagName('input');
//get all rows from the table
for ($i = 0; $i < $input_tags->length; $i++) {
if (is_object($input_tags->item($i))) {
$name = $value = '';
$name_o = $input_tags->item($i)->attributes->getNamedItem('name');
if (is_object($name_o)) {
$name = $name_o->value;
$value_o = $input_tags->item($i)->attributes->getNamedItem('value');
if (is_object($value_o)) {
$value = $input_tags->item($i)->attributes->getNamedItem('value')->value;
}
$post_data[$name] = $value;
}
}
}
return $post_data;
}
/*
Usage
*/
error_reporting(~E_WARNING);
function getauth(){
$html = curl_file_get_contents("http://forums.zybez.net/runescape-2007-prices/282-law+rune");
echo "<pre>";
$auth1 = (get_input_tags($html));
$auth = $auth1["auth"];
print_r($auth1);
}
getauth();
?>

PHP CURL post request and Error 417

function query($url, $pfields = 0, $cookie = 0)
{
curl_setopt($ch, CURLOPT_HEADER, 1);
if (!empty($pfields))
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pfields);
}
if (!empty($cookie))
{
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING,'gzip');
if (!$login)
{
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
}
$content = curl_exec($ch);
return $content;
}
$cookie = 'sessionID=3864cab58412ec567b634db3c317898;OAGEO=RU%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C;';
$p = '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++';
$post = 'clientid=23&campaignid=52&bannerid=111&appendsave=1&appendtype=0&append=' . urlencode($p) . '&submitbutton=';
echo query('http://example.com/in.php', $post, $cookie);
This code is returned 417 error(
BUT $p is not usage urlencode but IS OK but +(plus) change for " "(space)
Sooooorry for my very bad english
Try adding this:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

Categories