php curl shows non-readable signs - php

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');

Related

How to send POSTFIELDS to cURL correctly

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);

Would a misconfiguration in cookies and/or postfields lead to this 500 internal server error when using wp_remote_post to post data within Wordpress?

I am connecting to a server that does not have a published API and have successfully been able to use the PHP curl functions to retrieve a token and login, but when I try to use Wordpress built-in functions to achieve the same thing, I am not successful and receive a 500 Internal Server Error from the remote host. I believe that I may need to properly configure the cookies and/or data parameters.
I am able to retrieve a token from the remote host using wp_remote_get. I have tried changing the Content-Type to text, json and other common alternatives, although 'application/x-www-form-urlencoded' worked fine when using curl. I have tried sending the token in the header as well as in post fields.
// I am trying to get this to work:
$token = get_transient('access_token');
if (!$token)
return;
$args = array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'compress' => true,
'headers' => array(
'Authority' => 'remote.site.com',
'Content-Type' => 'application/x-www-form-urlencoded',
'Referer' => 'remote.site.com/login',
'Cache-Control' => 'max-age=0',
'Upgrade-Insecure-Requests' => 1,
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
'Accept-Encoding' => 'gzip, deflate, br',
'Accept-Language' => 'zh-CN,zh;q=0.9,en;q=0.8,ms;q=0.7,zh-TW;q=0.6,fr;q=0.5',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3'
),
'cookies' => array(),
'body' => array(
'__RequestVerificationToken' => $token,
'UserName' => USERNAME,
'Password' => PASSWORD,
'RememberMe' => true
)
);
$response = wp_remote_post( 'remote.site.com', $args);
// and no issue doing it this way...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'remote.site.com/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "__RequestVerificationToken=$token&UserName=JohnDoe&Password=password&RememberMe=true");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Authority: remote.site.com';
$headers[] = 'Cache-Control: max-age=0';
$headers[] = 'Origin: https://remote.site.com';
$headers[] = 'Upgrade-Insecure-Requests: 1';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8';
$headers[] = 'Referer: https://remote.site.com/login';
$headers[] = 'Accept-Encoding: gzip, deflate, br';
$headers[] = 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,ms;q=0.7,zh-TW;q=0.6,fr;q=0.5';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_HEADER, 1);
When using curl functions, I get 302 response for the login request and subsequent requests are fulfilled, whereas with wp_remote_post I get a response with the login page and a 500 internal server error response.
add code inside the wp-config file
define('COOKIE_DOMAIN', $_SERVER['HTTP_HOST'] );
define( 'ADMIN_COOKIE_PATH', '/' );
define( 'COOKIEPATH', '/' );
define( 'SITECOOKIEPATH', '/' );

Unable to get feed content with CURL

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;

PHP - make a request to cloudfare protected site?

I am attempting to download the document of a certain webpage(the stuff you would see if you hit 'view page source'), which has cloudfare protection, then echo it. Here is my current code:
<?php
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6',
CURLOPT_FOLLOWLOCATION => 0
);
curl_setopt_array($ch, $curlConfig);
echo(curl_exec($ch));
curl_close($ch);
?>
It redirects me to this url:
http://localhost/cdn-cgi/l/chk_jschl? jschl_vc=7df2b994a588cc3cb3b91ecc95acca7e&pass=1443965612.707-g3h0lQ531e&jschl_answer=377
Of course, I do not have chk_jschl on my localhost. How can I work around this?

PHP solution to JSON responses and parameters

I feel each step i take this one 5 more walls come up its fun but also frustrating.
So I'm trying to login to a website through cURL and after a lot of head scratching and the use of firefox developer tools I saw that my browser was responding in JSON. I honestly dont know anything about JSON but i feel like if i get through this I'll be in the clear.
So the question is how do i emulate these JSON responses with cURL? do i just copy and paste what they are in an array? Do i include this array in each http requests or do i do them all seperately.
Here is the JSON code if you guys want to see it.
__jsonp1__([{"id":"1","channel":"/meta/handshake","successful":true,"version":"1.0","supportedConnectionTypes":["long-polling","cross-origin-long-polling","callback-polling","websocket","eventsource","in-process"],"clientId":"adag3o01k7uyb0ub4s2v1h4r7fs1m3zfvp0","advice":{"reconnect":"retry","interval":0,"timeout":600000}}]);
__jsonp2__([{"id":"3","clientId":"adag3o01k7uyb0ub4s2v1h4r7fs1m3zfvp0","channel":"/meta/connect","successful":true,"advice":{"reconnect":"retry","interval":0,"timeout":600000}},{"id":"2","clientId":"adag3o01k7uyb0ub4s2v1h4r7fs1m3zfvp0","channel":"/meta/subscribe","successful":true,"subscription":"/user/11585628"}]);
__jsonp3__([{"id":"4","clientId":"adag3o01k7uyb0ub4s2v1h4r7fs1m3zfvp0","channel":"/meta/connect","successful":true,"advice":{"reconnect":"retry","interval":0,"timeout":600000}},{"channel":"/user/11585628","data":{"type":"subscribe"},"clientId":"adag3o01k7uyb0ub4s2v1h4r7fs1m3zfvp0","id":"5","authenticated":true}]);
_jsonp4__([{"id":"1","channel":"/meta/handshake","successful":true,"version":"1.0","supportedConnectionTypes":["long-polling","cross-origin-long-polling","callback-polling","websocket","eventsource","in-process"],"clientId":"uiqqkp0vf66rl0mlc8281ufknaw1qkcriu1","advice":{"reconnect":"retry","interval":0,"timeout":600000}}]);
here is my code I was trying somethings so thats why it make look a little weird
<?php
$ckfile =' __utma=173730677.1410450142.1370766442.1370882903.1370893342.8; __utmz=173730677.1370893342.8.6.utmcsr=web.groupme.com|utmccn=(referral)|utmcmd=referral|utmcct=/groups; __utma=64278953.892306882..1370882931.1370893339.9; __utmz=64278953.1370882931.8.4.utmcsr=groupme.com|utmccn=(referral)|utmcmd=referral|utmcct=/signin; _g=%3D%3D--772097f0c6a077ac0f904c981ba5523ddffef3d5; __utmc=64278953; __utmc=173730677; __utmb=64278953.1.10.1370893339; __utmb=173730677.2.10.1370893342';
$postfields = '{"username":"#gmail.com","password":"somepass","app_id":"groupme.com","grant_type":"password"}';
$postfields2 ='{"group":{"name":"test","memberships":[]}}';
$custom = 'X-Access-Token: CEbhaIkkKTc9dtVMpxyc2IZOfnzEoh5w4UTzsVSb';
$ch2 = curl_init();
$ch3 = curl_init();
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => 'https://web.groupme.com/#access_token=some token',
CURLOPT_COOKIE=> $ckfile,
CURLOPT_USERAGENT =>'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0',
CURLOPT_REFERER => 'https://groupme.com/signin',
CURLOPT_RETURNTRANSFER => true,
//CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER => array('Host: web.groupme.com','Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language: en-US,en;q=0.5','Accept-Encoding: gzip, deflate')
)
);
curl_setopt_array(
$ch2,
array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => 'https://web.groupme.com/#access_token=some token',
CURLOPT_COOKIE=> $ckfile,
CURLOPT_USERAGENT =>'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0',
CURLOPT_REFERER => 'https://groupme.com/signin',
CURLOPT_RETURNTRANSFER => true,
//CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER => array('Host: web.groupme.com','Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language: en-US,en;q=0.5','Accept-Encoding: gzip, deflate','Content-Type=text/html;charset=utf-8','Server=thin 1.3.1 codename Triple Espresso','Strict-Transport-Security=max-age=31536000','X-Frame-Options=sameorigin','x-xss-protection=1; mode=block','Content-Length=24275')
)
);
$response = curl_exec($ch2);
//curl_close($ch);
echo '<pre>';
print_r($response);
echo '</pre>'
?>

Categories