Curl with Post not working Php ? - php

well the problem is the simple this today not working !
<?php
function cload($url, $got = array()){
//include(MDL.'socket_adapter.php');
$user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0";
$default_options = array(
'data' => 'og',
'post_data' => false,
'referer' => false,
'cookie' => false,
'auth' => false,
'proxy' => false,
'pauth' => false,
'returndata' => true,
);
foreach($default_options as $opt=>$value) {
if(!empty($default_options[$opt])) {$got[$opt] = $value; }
//if(!empty($got[$opt])) {$got[$opt] = $value; }
}
//echo "<hr>"; print_r($got); echo "<hr>";
$curl = curl_init();
//if(strstr($referer,"://")){
//curl_setopt ($curl, CURLOPT_REFERER, $got['referer']);}
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($curl, CURLOPT_HEADER, 1);
if(isset($got['returndata'])){
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
}else{
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, false);
}
curl_setopt ($curl, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, false);
if(isset($got['post_data'])){ curl_setopt($curl ,CURLOPT_POST , true);
curl_setpot($curl ,CURLOPT_POSTFIELDS ,$got['post_data']); }
if(isset($got['referer'])) curl_setopt($curl,CURLOPT_REFERER, $got['referer']);
if(isset($got['cookie'])){ if($got['cookie'] == "0"){
curl_setopt($curl ,CURLOPT_COOKIEJAR, TBP."cookie.txt"); }//If ever needed...
else{
curl_setopt($curl ,CURLOPT_COOKIE, $got['cookie']); }}
$custom_headers = array();
$custom_headers[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$custom_headers[] = "Pragma: no-cache";
$custom_headers[] = "Cache-Control: no-cache";
$custom_headers[] = "Accept-Language: en-us;q=0.7,en;q=0.3";
$custom_headers[] = "Accept-Charset: utf-8,windows-1251;q=0.7,*;q=0.7";
if(isset($url_parts['user']) and isset($url_parts['pass'])) {
$custom_headers[] = "Authorization: Basic ".base64_encode($url_parts['user'].':'.$url_parts['pass']);
}elseif(isset($got['auth'])){
$uj = explode(":",$got['auth']); $custom_headers[] = "Authorization: Basic ".base64_encode($uj[0].':'.$uj[1]); }
if(isset($got['pauth'])){
curl_setopt($curl ,CURLOPT_PROXYUSERPWD ,$pauth); }
if(isset($got['proxy'])){
curl_setopt($curl ,CURLOPT_PROXY ,$proxy); }
curl_setopt($curl ,CURLOPT_HTTPHEADER, $custom_headers);
//curl_setopt($curl, CURLINFO_HEADER_OUT, true);
$response = curl_exec($curl);
$info = curl_getinfo($curl);// , CURLINFO_HEADER_OUT);
curl_close ($curl);
//echo "<hr>"; echo $info; echo "<hr>";
return $response; }
?>
now when i am using this code :
<?php
$cv = load('https://localhost/a/ac.php?a=io',array('cookie' => $fbcook,'referer' => $ref,'post_data' => 'odl=lop&isi=837&io'));
echo $cv;
?>
but i am getting error :
Fatal error: Call to undefined function curl_setpot() in C:\xampp\htdocs\a\mack\curl.php on line 41
Please help me ! why this happning this line 42 indicates this line curl_setpot($curl ,CURLOPT_POSTFIELDS ,$got['post_data']); }
thanks

simple typo: curl_setpot should be curl_setopt

When a function of cURL is undefined, then it is most likely that cURL is not available.
However, I think that you meant curl_setopt().

curl_setpot should be curl_setopt.

You use xampp so you need to activate curl lib by following these steps:
Locate XAMPP install directory
Open php/php.ini (probably C:\xampp\php\php.ini
or C:\program files\apachefriends\xampp\php\php.ini)
Do a search for the word ‘curl’ and uncomment (remove the leading semicolon) that line. Before removing: ;extension=php_curl.dll. After removing: extension=php_curl.dll
Save and close
Open apache/bin/php.ini (probably C:\xampp\apache\php.ini
or C:\ program files\apachefriends\xampp\apache\php.ini)
Search for curl, uncomment as before (step 3)
Save and close
Do not forget to restart Apache
Good luck

Related

how to loop through ann array with curl function?

I can't loop through an array, stil get Curl error "3"
i have checked and inside array are all links I wanted to get.
function to make a get request
<?php
function geturl($url){
$starttime = microtime(TRUE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0)
Gecko/20100101 Firefox/47.0");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
curl_setopt($ch, CURLOPT_HTTPHEADER,
[
'https://betsapi.com/',
'authority: betsapi.com',
'upgrade-insecure-requests: 1',
'accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,
application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site: cross-site' ,
'sec-fetch-mode: navigate',
'sec-fetch-user: ?1',
'sec-fetch-dest: document',
//'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="98", "Microsoft Edge";v="98"',
'sec-ch-ua-mobile: ?0',
'sec-ch-ua-platform: "Windows"',
'referer: https://betsapi.com/',
'accept-language: pl,en;q=0.9,en-GB;q=0.8,en-US;q=0.7',
]);
$response = curl_exec($ch);
here i check errors and request time
if(curl_errno($ch))
{
echo "Curl error no: ". curl_errno($ch);
exit;
}
curl_close($ch);
$endtime = microtime(TRUE);
echo "Request time"." ". $endtime - $starttime."\n";
return $response;
}
function to load html
function simplehtml($item){
include_once('simple_html_dom.php');
$html = new simple_html_dom();
$html->load($item);
return $html;
}
when i call for the first time the geturl function and simplehtml function it works and puts all links into array
$starturl= geturl('https://betsapi.com/cin/soccer');
$starturlhtml= simplehtml($starturl);
foreach($starturlhtml->find("tr[class='c_1'] td[class='text-center'] a")as $url){
$url="https://betsapi.com".$url->href."\n";
$urlarray[]=$url;
}
here i call it in loop and i get Curl error 3
foreach($urlarray as $urls){
$urlss= geturl($urls);
$urlshtml=simplehtml($urlss);
foreach($urlshtml->find("div[class='col-md-6 text-center'] p b")as $stadium){
echo $stadium;
sleep(2);
}
}

LendingClub.com API Internal server Error for Buying Notes on Secondary Market

I am attempting to buy Notes of the secondary market of Lending club and I keep getting "Internal server error". I have asked Lending club support multiple times too, but they are clueless. I also tried following this post , but no luck LendingClub.com API 500 Error for Buying Notes on Secondary Market.
Please help
<?php
$invester_id = "516xxxxxx";
$api_key = "GVsZuxxxxxxxxx";
$ContentType = "application/json";
define("DEBUG_LENDING_API", true);
$buy = buy_notes($invester_id, $api_key);
print_r($buy);die;
function buy_notes($invester_id, $api_key){
$buy_notes_url = "https://api.lendingclub.com/api/investor/v1/accounts/$invester_id/trades/buy";
$note = array("loanId" => "97277470", "orderId" => "139320895", "noteID" => "149206918", "bidPrice" => "19.45");
$datas = array("aid" => "70654", "notes" => $note);
$buy_notes = call_curl($buy_notes_url, $api_key, json_encode($datas));
$notes = json_decode($buy_notes['data']);
return $notes;
}
function call_curl($url, $api_key, $post = "0"){
$invester_id = "516xxxxxx";
$ContentType = "application/json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0" );
if($post != "0"){
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $post);
}
curl_setopt ( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, true );
$headers = array();
$headers[] = "Authorization: $api_key";
$headers[] = "Content-type: $ContentType";
$headers[] = "Accept: $ContentType";
$headers[] = "X-LC-Application-Key: $invester_id";
//print_r(array_values($headers));
//exit;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
echo $server_output. "<br>";
exit;
$info = curl_getinfo($ch);
curl_close ($ch);
if(DEBUG_LENDING_API == true){
return array("data" => $server_output, "response" => $info);
}else{
return json_decode($server_output);
}
}
?>
I managed to figure it out. I set "aid" to $invester_id and that finally did the trick. Thanks Aynber for your tips along the way.

php how add pause in script?

We are have a code:
<?php
function cURL_AutonavigatorRu($level = false, $model_id = false){
#http://www.autonavigator.ru
$ch = curl_init('http://www.autonavigator.ru/dispatcher.pl');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36");
$headers = array
(
'Accept: application/json',
'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
'Accept-Encoding: gzip,deflate',
'Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7'
);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_REFERER, "http://www.autonavigator.ru/my/offer_add/");
if($level == '1'){
curl_setopt($ch, CURLOPT_POSTFIELDS, 'class=list&method=make&show_all=1&vehicle=car&type=used');
}
elseif($level == '2' && $model_id){
curl_setopt($ch, CURLOPT_POSTFIELDS, 'class=list&method=model&show_all=1&vehicle=car&type=used&make_id='.$model_id);
}
elseif($level == '3' && $model_id){
curl_setopt($ch, CURLOPT_POSTFIELDS, 'class=list&method=modif&show_all=1&model_id='.$model_id);
}
else{
curl_setopt($ch, CURLOPT_POSTFIELDS, 'class=list&method=modif&show_all=1&model_id='.$model_id);
}
curl_setopt($ch, CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$json = json_decode(iconv("windows-1251","utf-8",$result), true);
return $json['list'];
}
$ArrAuto = cURL_AutonavigatorRu('1');
foreach($ArrAuto as $auto) {
echo $auto['value'].'<br>';
//sleep(2);
$AllModif1 = cURL_AutonavigatorRu('2',$auto["id"]);
var_dump($AllModif1);
echo '<br><br>----------------------------------<br><br>';
}
We are have problem - web site block curl and not give results for each $AllModif1 = cURL_AutonavigatorRu('2',$auto["id"]); in one time(in curl we get null).
Tell me please how make to pause the script that cycle foreach($ArrAuto as $auto) worked every 5 seconds?
P.S.: How make pause 4 secound between cycle?
P.P.S.: we are know about sleep() but i not get result with it see please prntscr.com/4ylm9y
Use sleep:
foreach($ArrAuto as $auto){
//Your amazing code here
sleep(4);
}
You should check the sleep() function
Add sleep(4); in your foreach loop.
just add sleep(5); in your foreach()
Add the following:
sleep(4);
More info here: http://php.net/manual/en/function.sleep.php

Curl not working correctly

<?php
function cuload($url, $got = array()){
//include(MDL.'socket_adapter.php');
$user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0";
$default_options = array(
'data' => 'og',
'post_data' => false,
'referer' => false,
'cookie' => false,
'auth' => false,
'proxy' => false,
'pauth' => false,
'returndata' => true,
);
foreach($default_options as $opt=>$value) {
if(!isset($default_options[$opt])) {$got[$opt] = $value; }
}
$curl = curl_init();
//if(strstr($referer,"://")){
//curl_setopt ($curl, CURLOPT_REFERER, $got['referer']);}
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($curl, CURLOPT_HEADER, 1);
if(isset($got['returndata'])){
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
}else{
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, false);
}
curl_setopt ($curl, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
//curl_setopt($curl ,CURLOPT_USERAGENT, $got_opt['user_agent']); //The Name of the UserAgent we will be using ;)
if(isset($got['post'])){curl_setopt($curl ,CURLOPT_POST , true); curl_setpot($curl ,CURLOPT_POSTFIELDS , $post); }
if(isset($got['referer'])) curl_setopt($curl,CURLOPT_REFERER, $options['referer']);
if(isset($got['cookie'])){ if($got['cookie'] == "0"){
curl_setopt($curl ,CURLOPT_COOKIEJAR, TBP."cookie.txt"); }//If ever needed...
elseif($got['cookie'] != "0"){
curl_setopt($curl ,CURLOPT_COOKIE, $got['cookie']); }}
//curl_setopt($curl ,CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($curl ,CURLOPT_MAXREDIRS, 5);
//curl_setopt($curl ,CURLOPT_SSL_VERIFYPEER, false);
$custom_headers = array();
$custom_headers[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$custom_headers[] = "Pragma: no-cache";
$custom_headers[] = "Cache-Control: no-cache";
$custom_headers[] = "Accept-Language: en-us;q=0.7,en;q=0.3";
$custom_headers[] = "Accept-Charset: utf-8,windows-1251;q=0.7,*;q=0.7";
if(isset($url_parts['user']) and isset($url_parts['pass'])) {
$custom_headers[] = "Authorization: Basic ".base64_encode($url_parts['user'].':'.$url_parts['pass']);
}elseif(isset($got['auth'])){
$uj = explode(":",$got['auth']); $custom_headers[] = "Authorization: Basic ".base64_encode($uj[0].':'.$uj[1]); }
if(isset($got['pauth'])){
curl_setopt($curl ,CURLOPT_PROXYUSERPWD ,$pauth); }
if(isset($got['proxy'])){
curl_setopt($curl ,CURLOPT_PROXY ,$proxy); }
curl_setopt($curl ,CURLOPT_HTTPHEADER, $custom_headers);
$response = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close ($curl);
ob_end_clean();
return $response;
}
?>
ok now when i tries this script on same page :
<?php
$page = cuload('http://www.google.com');
?>
But now without echoing the $page variable it echo the content . i donot need to echo anything here is just need to find some link from the $page variable content .. after exploring the script deeply i find that may be there was a problem in curl_exec cause if i remove the return $response still is shows the content ! i cant understand why it giving output of $page when i am not echoing $page .
please help me !
Instead of
foreach($default_options as $opt=>$value) {
if(!isset($default_options[$opt])) {$got[$opt] = $value; }
}
try this:
foreach($default_options as $opt=>$value) {
if(!empty($default_options[$opt])) {$got[$opt] = $value;}
}

file_get_contents script works with some websites but not others

I'm looking to build a PHP script that parses HTML for particular tags. I've been using this code block, adapted from this tutorial:
<?php
$data = file_get_contents('http://www.google.com');
$regex = '/<title>(.+?)</';
preg_match($regex,$data,$match);
var_dump($match);
echo $match[1];
?>
The script works with some websites (like google, above), but when I try it with other websites (like, say, freshdirect), I get this error:
"Warning: file_get_contents(http://www.freshdirect.com) [function.file-get-contents]: failed to open stream: HTTP request failed!"
I've seen a bunch of great suggestions on StackOverflow, for example to enable extension=php_openssl.dll in php.ini. But (1) my version of php.ini didn't have extension=php_openssl.dll in it, and (2) when I added it to the extensions section and restarted the WAMP server, per this thread, still no success.
Would someone mind pointing me in the right direction? Thank you very much!
It just requires a user-agent ("any" really, any string suffices):
file_get_contents("http://www.freshdirect.com",false,stream_context_create(
array("http" => array("user_agent" => "any"))
));
See more options.
Of course, you can set user_agent in your ini:
ini_set("user_agent","any");
echo file_get_contents("http://www.freshdirect.com");
... but I prefer to be explicit for the next programmer working on it.
$html = file_get_html('http://google.com/');
$title = $html->find('title')->innertext;
Or if you prefer with preg_match and you should be really using cURL instead of fgc...
function curl($url){
$headers[] = "User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
$headers[] = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$headers[] = "Accept-Language:en-us,en;q=0.5";
$headers[] = "Accept-Encoding:gzip,deflate";
$headers[] = "Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$headers[] = "Keep-Alive:115";
$headers[] = "Connection:keep-alive";
$headers[] = "Cache-Control:max-age=0";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_ENCODING, "gzip");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
$data = curl('http://www.google.com');
$regex = '#<title>(.*?)</title>#mis';
preg_match($regex,$data,$match);
var_dump($match);
echo $match[1];
Another option: Some hosts disable CURLOPT_FOLLOWLOCATION so recursive is what you want, also will log into a text file any errors. Also a simple example of how to use DOMDocument() to extract the content, obviously its not extensive but something you could build appon.
<?php
function file_get_site($url){
(function_exists('curl_init')) ? '' : die('cURL Must be installed. Ask your host to enable it or uncomment extension=php_curl.dll in php.ini');
$curl = curl_init();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: ";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0 Firefox/5.0');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_REFERER, $url);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
$html = curl_exec($curl);
$status = curl_getinfo($curl);
curl_close($curl);
if($status['http_code']!=200){
if($status['http_code'] == 301 || $status['http_code'] == 302) {
list($header) = explode("\r\n\r\n", $html, 2);
$matches = array();
preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
$url = trim(str_replace($matches[1],"",$matches[0]));
$url_parsed = parse_url($url);
return (isset($url_parsed))? file_get_site($url):'';
}
$oline='';
foreach($status as $key=>$eline){$oline.='['.$key.']'.$eline.' ';}
$line =$oline." \r\n ".$url."\r\n-----------------\r\n";
$handle = #fopen('./curl.error.log', 'a');
fwrite($handle, $line);
return FALSE;
}
return $html;
}
function get_content_tags($source,$tag,$id=null,$value=null){
$xml = new DOMDocument();
#$xml->loadHTML($source);
foreach($xml->getElementsByTagName($tag) as $tags) {
if($id!=null){
if($tags->getAttribute($id)==$value){
return $tags->getAttribute('content');
}
}
return $tags->nodeValue;
}
}
$source = file_get_site('http://www.freshdirect.com/about/index.jsp');
echo get_content_tags($source,'title'); //FreshDirect
echo get_content_tags($source,'meta','name','description'); //Online grocer providing high quality fresh......
?>

Categories