Generate URL with Variable - php

Hopefully, someone can spot the error, what I need to do is to first fetch the webpage for a token, then curl the new url with the token attached;
here is my code
$text = $siteName;
if (preg_match('/;t=([a-zA-Z0-9_-]{43})%3D/',$text,$matches)) {
// Match... vjVQa1PpcFMYuRsz10_H-1z41mWWe8d6ENEnBLE7gug
echo 'TOKEN: '.$matches[1];
$curltube = curl_init ();
curl_setopt ($curltube, CURLOPT_URL, "http://www.veoh.com/watch?v=opQ9GzRe5qs".$matches[1]);
curl_setopt ($curltube, CURLOPT_RETURNTRANSFER, 0 );
curl_setopt ($curltube, CURLOPT_COOKIEFILE, "cookie8.txt");
$curltubeplay = curl_exec ($curltube);
curl_close ($curltube);
echo $curltubeplay;
} else {
// No match
}
and the previous code before that fetches the web-page
curl_setopt ($ch, CURLOPT_URL, "http://www.veoh.com/watch?v=opQ9GzRe5qs");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 0 );
curl_setopt ($ch, CURLOPT_COOKIEJAR, "cookie8.txt");
so hopefully, someone can shed some light

My guess (please expand the question to be clear) is that you expect to build a URL like this:
http://www.veoh.com/watch?v=opQ9GzRe5qs;t=abc
But you are building one like this:
http://www.veoh.com/watch?v=opQ9GzRe5qsabc
There are two simple fixes. This one takes the whole matched string, not just the token:
curl_setopt ($curltube, CURLOPT_URL, "http://www.veoh.com/watch?v=opQ9GzRe5qs".$matches[0])
And this one adds back in the missing parts to the URL:
curl_setopt ($curltube, CURLOPT_URL, "http://www.veoh.com/watch?v=opQ9GzRe5qs;t=".$matches[1])

Related

cURL PHP Getting value of JSON

Response:
{
"error":null,
"value1":"STRING",
"valuelist":{
valueurl:{
"status":STATUS_TEXT,
},
}
}
cURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $response);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
$content = curl_exec ($ch);
curl_close ($ch);
return $content;
now I have json_decoded it, I can't get value of "status".
echo $foo->value1; works fine.
echo $foo->status; isn't right. How would I get that value?
The value of status should be access like below:
$foo->valuelist->valueurl->status
When sometimes you don't know where is certain property or index you can always maka a var_dum() de variable, example:
var_dump(json_decode($content));
So you can see the tree structure:
$foo->valuelist->valueurl->status

Remote server returns error page after sending credentials

I'm trying to create simple webpage that allows me to check current schedule from information system. IS is written in ASP.NET and using library to generate random name values for inputs. Also it uses one-time log in token.
I wrote this script to log in and fetch current schedule.
<?php
function curl($url, $post=false,$cookie = 'cookie.txt'){
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url );
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt ($ch, CURLOPT_AUTOREFERER, true);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
if($cookie){
$agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36";
curl_setopt ($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
}
if($post){
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
}
return curl_exec ($ch);
}
// require simple html dom library to work with html later
require 'simple_html_dom.php';
// login page uri
$login_page = "http://bakalari.sosklobouky.cz:82/login.aspx";
// fetch login page
$ch = curl($login_page ,false);
// create simple html dom to read hidden inputs, etc.
$html = str_get_html($ch);
// add all hidden inputs into string
$hidden_inputs = '';
foreach ($html->find('input[type=hidden]') as $element) {
$hidden_inputs .= '&'.$element->name.'='.$element->value;
}
// fetch actual name atributes of username and password form
$username_name = $html->find('input[tabindex=1]', 0)->name;
$password_name = $html->find('input[tabindex=2]', 0)->name;
// credentials
$username = "9703042329z";
$password = "somepass";
// post data - credentials + hidden inputs
$postdata = $username_name . '=' . $username . '&' . $password_name . '=' . $password . $hidden_inputs;
// send request to login page with credentials
$ch = curl($login_page ,$postdata);
// print result
echo $ch;
// try to fetch page that is available after login
$ch = curl('http://bakalari.sosklobouky.cz:82/uvod.aspx',false);
// print return
echo $ch;
?>
But remote server returns error of application. Do you have any ideas? Maybe I'm sending postdata badly. I'm not sure, because I'm writing script like this one for first time.
Close the curl handle, otherwise it will not save cookie for every call(It will only when the program ends).
$result = curl_exec ($ch);
curl_close($ch);
return $result;
From the log and the latest conversation, here is the UPDATE
You are trying to POST a large number of data(currently 1155 bytes). And through normal post, you can not pass more than 1024 bytes data. And hence you are getting 100 Continue response from the site.
So, convert your post into multipart/form-data and you'll get passed. Here is how you can do it.
Make an array with all your post values.
$post_data = array(
'name' => 'some name',
'pass' => 'some pass',
'hidden-1' => 'some hidden-1', // you may need to do urlencode() here, I am not sure
'others-1' => 'some others 1'
/// and so on
);
Then post the array directly into the curl post like below:
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_data);

PHP cURL No Error but not working

I've been banging my head on this for a while now and I'm sorta fed up. I'm not a PHP programmer so I might be missing something that is not immediately obvious to my Python infused brain.
So here's the context. I need to write a script to login automatically into a web interface and run a search, and well, absolutely everything I've tried to do seems to fail miserably.
Here's my code:
<?php
echo 'start';
// INIT CURL
$ch = curl_init();
// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'https://W0110DcIpsOcsRpt02/si_ocs_gui/login.php');
// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);
// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=*****&password=*****&btnSubmit=Login');
// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, '/var/tmp/cookie.txt');
# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
# not to print out the results of its query.
# Instead, it will return the results as a string return value
# from curl_exec() instead of the usual true/false.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_FRESH_CONNECT, 1);
// EXECUTE 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);
if($store == False){
echo " store false ";
echo curl_error($ch);
} else {
echo " store true ";
echo $store;
}
// SET FILE TO DOWNLOAD
echo ' second_request ';
curl_setopt($ch, CURLOPT_URL, 'https://W0110DcIpsOcsRpt02/si_ocs_gui/FWOCS1_BL_SUBSCRIBERS_list.php');
curl_setopt ($ch, CURLOPT_POST, 1);
#curl_setopt ($ch, CURLOPT_POSTFIELDS, 'value_ACCESS_NO_4=15142351150');
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'q=\(SUBSCRIBER_ID~equals~1545303\)');
// EXECUTE 2nd REQUEST (FILE DOWNLOAD)
if(!$store == False){
$content = curl_exec ($ch);
echo $content;
}
// CLOSE CURL
curl_close($ch);
echo ' done';
?>
The behaviour of this is as follows. I run the script, and it fails at the first cURL request (the login) and returns no errors. I have modified this line here:
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=*****&password=*****&btnSubmit=Login');
by removing the btnSubmit=Login part and I get the login page displayed with the filled in username and password fields. I can press the login button and it will work. However, it seems that my lack of web development is biting me here and I have no idea how that button works. So, here's the code inspected with firebug:
<a class="rnr-button main" href="#" onclick="document.forms[0].submit();return false;">Submit</a>
I also went directly in the PHP code on the web server and found the button corresponding to it:
if ((#$_POST["btnSubmit"] == "Login" || $adSubmit) && $logacc)
Hence why I was trying the btnSubmit=Login.
So my question is pretty simple: what am I doing wrong and how can I get the results I need?
I have got the same problem and find the solution for it.
Code
CURLOPT_RETURNTRANSFER => false
Use this to solve your problem.

Posting to website using curl

This may be another dumb question but anyway. I am trying to post into a search box of the University online public access catalogue and here is what I came up with:
<?php
date_default_timezone_set('Asia/Manila');
$today = date('m-d-Y');
echo $today;
$keyWord=database;
$urltopost = "http://opac.usls.edu.ph/TLCScripts/interpac.dll?SearchForm? Directions=1&Config=pac&Branch=0";
$datatopost = array ('SearchData' => "c++");
$ch = curl_init ($urltopost);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$returndata = curl_exec ($ch);
echo $returndata;
?>
I get this:
09-19-2013
Configuration has been deleted.
I really need help. What did I do wrong?
Thanks in advance good people of this world.
If you look at how a search is done in that site, you can see that your post url should be
http://opac.usls.edu.ph/TLCScripts/interpac.dll?Search
and data should be something like
FormId=0&Config=pac&LimitsId=0&StartIndex=0&SearchField=7&SearchType=1&ItemsPerPage=10&SearchData=yoursearchtermhere
Since in your example you're not posting the "config" parameter in post data, you get the error message you specified.

Using curl to bring search results from external site

I have 2 sites, one main, one external. On the main site, I am using Lucene to search through it. The problem is, I am trying to also search through the external site.
The Form action for the external site:
<form action="https://secure.bcchf.ca/SuperheroPages/searchResults.cfm?Event=WOT" method="post" name="search_tribute" >
I've tried to use curl, but it only brings up the search form without actually doing the search (the field is empty as well).
<?php
$ch = curl_init("https://secure.bcchf.ca/SuperheroPages/searchResults.cfm?Event=WOT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, tname='hello');
$output = curl_exec($ch);
echo $output;
curl_close($ch);
?>
Any tips?
I don't have access to the form action since it's on an external site. All i have is a form that links to it when I submit it.
<?php
$ch = curl_init("https://secure.bcchf.ca/SuperheroPages/searchResults.cfm?Event=WOT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("teamName" => "hello", "searchType" => "team"));
$output = curl_exec($ch);
echo $output;
curl_close($ch);
?>
Can you try this?
I'm pretty sure it's supposed to be teamName instead of tName
Most search engine use GET and not POST .. you can try
// asumption
$_POST['search'] = "hello";
// Return goole Search Result
echo curlGoogle($_POST['search']);
function curlGoogle($keyword) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/search?hl=en&q=' . urlencode($keyword) . '&btnG=Google+Search&meta=');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILETIME, true);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Or if you want post then
curl_setopt($ch, CURLOPT_POSTFIELDS, array("search"=>"hello"));
Your php code is not valid syntax, it does not compile.
So if this is really what you have, your problem is that your file generates a fatal error.
That being said, this question is hard to answer since we don't know the site you want to grab your search results from.
Try modifying your line like this:
curl_setopt($ch, CURLOPT_POSTFIELDS, "search=hello");
or alternatively
curl_setopt($ch, CURLOPT_POSTFIELDS, array("search" => "hello");
Maby it will work, however it may be that more post data is required or that the element name is not correct.
You have to look at the form or try making a request and look at it with chromes developer tools or firebug.
Also there are a number of ways for external sites to prevent what you are doing, altough evertything can be worked around somehow.
Assuming that is not the case, I hope i could help you.
Try just putting it into an array.
as that will be the variable the $_POST checks on the other side
and just checked your link, its teamName for the field
$fields = array("teamName"=>"julia");
Then..
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
So your complete code is...
<?php
$ch = curl_init("https://secure.bcchf.ca/SuperheroPages/searchResults.cfm?Event=WOT");
$fields = array("teamName"=>"julia");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$output = curl_exec($ch);
var_dump($output);
curl_close($ch);
?>

Categories