I'm trying to get the content of this feed :
http://www.institut-viavoice.com/viavoice-paris/publications/sondages-publies?format=feed&type=rss
Here is my code :
$url = 'http://www.institut-viavoice.com/viavoice-paris/publications/sondages-publies?format=feed&type=rss';
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_TIMEOUT => 30,
CURLOPT_MAXREDIRS => 10
);
$curl = curl_init($url);
curl_setopt_array( $curl, $options );
$content = curl_exec($curl);
curl_close($curl);
echo $content;
I tried many other CURL options but it doesn't work.
As the content is accessible through my browser, I suppose it can be done with PHP. But what is wrong with my code ? It seems like there is an exception with the server of this feed ?
Not sure, may be your breaking the cURL options and calling the URL.
Here a simple example, give it a try:
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$content = get_data('http://www.institut-viavoice.com/viavoice-paris/publications/sondages-publies?format=feed&type=rss');
echo $content;
Related
I need to send POST data via cURL as shown in the picture.
image with POST data
i have this code
$data = [
'action' => 'order_cost',
'address' => 'http://91.211.117.3:720'
];
$query = http_build_query($data);
$url = "https://ap4.taxi/api/TaxiAPI.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvJFySHvqeKppEN9W',
)
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$output = curl_exec($ch);
curl_close($ch);
var_dump($output);
but I get an error
image with error
I have already tried many options. Postman sends POST normally and I receive the answer.
Please tell me I can not even imagine how this can be done.
As I see from your code you are sending just two fields by POST method (action and address)
Please show us a code of https://ap4.taxi/api/TaxiAPI.php where you process received data.
function execute_curl($url, $curlopt = array()){
$ch = curl_init();
$strCookie = session_name().'='.session_id().'; path=/';
session_write_close();
$default_curlopt = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_COOKIE => $strCookie,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 AlexaToolbar/alxf-1.54 Firefox/3.6.13 GTB7.1"
);
$curlopt = $curlopt + $default_curlopt;
curl_setopt_array($ch, $curlopt);
$response = curl_exec($ch);
$errormsg = curl_error($ch);
$errorCode = curl_errno($ch);
$results = array();
if($errormsg)
{
$results['status'] = 'error';
$results['data'] = $errormsg;
$results['errorcodetxt'] = curl_error_codes($errorCode);
}
else
{
$results['status'] = 'success';
$results['data'] = $response;
}
curl_close($ch);
return $results;
}
$curlopt = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => 1);
$curlresponse = execute_curl($url, $curlopt);
This simple request works on my console
curl 'https://www.nike.com/en'
But in PHP (with or without options like headers, useragent ...) I get Access Denied 403
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.nike.com/en");
$result = curl_exec($ch);
Thanks
Well, this would work for you.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.nike.com/en");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 40000);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
echo $result;
<?php
class Curl {
public static function makeRequest($url) {
$ch = curl_init();
$request_headers = [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;',
'Accept-Encoding: gzip, deflate',
"Connection: keep-alive",
"Content-Type: text/html; charset=UTF-8",
];
$options = [
CURLOPT_URL => $url,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_USERAGENT => "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0",
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_AUTOREFERER => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_FILETIME => true,
CURLOPT_FRESH_CONNECT => true,
CURLOPT_HTTPHEADER => $request_headers,
CURLOPT_COOKIESESSION => true,
CURLOPT_ENCODING => "gzip, deflate, scdh",
];
curl_setopt_array($ch, $options);
$result['content'] = curl_exec($ch);
$result['header'] = curl_getinfo($ch);
$result['error'] = curl_error($ch);
return $result;
}
}
var_dump(Curl::makeRequest('https://www.nike.com/en')['header']);
Some website strictly check the request headers and return you encoded so .. I added request headers ad decoding method to decode the content
my below code of PHP is not working. I have created test user id & password which is shared in the code. The file is not having any value. But I see with this code it is edited every time when I execute this code.
<?php
$username = '8632465';
$password = 'basade41';
$loginUrl = 'https://cabinet.instaforex.com/partner/login';
//$userAgent = 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0';
$config['useragent'] = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0';
//$userAgent = $_SERVER['HTTP_USER_AGENT'];
//init curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']);
//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $loginUrl);
// ENABLE HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);
//Set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user='.$username.'&pass='.$password);
//Handle cookies for the login
curl_setopt($ch, CURLOPT_COOKIEJAR, '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);
//execute the request (the login)
$store = curl_exec($ch);
//the login is now done and you can continue to get the
//protected content.
//set the URL to the protected file
curl_setopt($ch, CURLOPT_URL, 'https://cabinet.instaforex.com/partner/aff_statistic');
//execute the request
$content = curl_exec($ch);
//save the data to disk
file_put_contents('c:/xampp/htdocs/upload/test.txt', $content);
?>
I think your code missing some settings. eg:CURLOPT_CAINFO...
This is my https code
<?php
//The CA certificate path
$caDir = '....../GeoTrustGlobalCA.crt';
//cookie file path
$cookieDir = '......';
//URL
$url = 'https://www.zhihu.com/......';
$defaultOpt = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36",
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => true, // enabled SSL Cert checks
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_CAINFO => $caDir,
CURLOPT_COOKIEFILE => $cookieDir,
CURLOPT_COOKIEJAR => $cookieDir,
CURLOPT_URL => $url,
CURLOPT_HTTPGET => true //method is GET
);
$handle = curl_init();
curl_setopt_array($handle,$arrOpt);
$content = curl_exec($handle);
$err = curl_errno($handle);
$errmsg = curl_error($handle);
$header = curl_getinfo($handle);
curl_close($handle);
printf("Content is %s", $content);
?>
you can refer to this article php-curl-https (in (^__^)) OR this using-curl-in-php-to-access-https-ssltls-protected-sites
My Curl-code works:
$ch = curl_init();
$header = array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Accept-Language: en-us;q=0.8,en;q=0.6'
);
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13',
CURLOPT_HTTPHEADER => $header
);
curl_setopt_array($ch, $options);
$str = curl_exec($ch);
curl_close($ch);
But from one page only I get signs start so:
‹�������í}ëvâH²îïîµö;dk¦}™² ...
I detect with mb_detect_encoding that it is an utf8-string. I'm confused, why can't I read the source code like from other sites?
The site I want to curl is http://stores.ebay.com/artlines-2012
Thank you.
It looks like an encoding issue. Try adding CURLOPT_ENCODING.
Example:
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
How i can redirect user to another web site with cookie?
Im using this code
<?php
$fields_string = 'client_login=jadro&client_pass=jadro&client_remember=on&action=client_login';
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 FirePHP/0.3",
CURLOPT_AUTOREFERER => false,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
);
$ch = curl_init();
curl_setopt_array( $ch, $options );
curl_setopt($ch,CURLOPT_URL,'http://orion10.ru');
//curl_setopt($ch,CURLOPT_POST,count(explode('&',$fields)));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//curl_setopt($ch,CURLOPT_COOKIEJAR, 'cooc.txt');
//curl_setopt($ch,CURLOPT_COOKIEFILE, 'cooc.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, getcwd()."/cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, getcwd()."/cookies.txt");
$result = curl_exec($ch);
echo $result;
//header("Location: http://orion10.ru".session_name().'='.session_id());
header('Refresh: 15; URL='.$url['http://orion10.ru']);
exit();
?>
I need authorize user to another site.
cURL is being executed on your server. Therefore, the website in question thinks that your server is the user. i.e. When you redirect the actual user to the website in question, it won't recognize them. Read this.