PHP Curl post with non-english characters - php

I have a small problem with php curl post.
I am trying to post some turkish characters to a forum but aren't posted how it should be.
This is how i save the text:
fwrite($fpp,"\xEF\xBB\xBF");
fwrite($fpp, $row['template']);
fclose($fpp);
and posting:
$this->curl = curl_init();
curl_setopt ( $this->curl, CURLOPT_URL, $this->vb_url . 'newthread.php?' . $url_vars );
curl_setopt ( $this->curl, CURLOPT_POST, true );
curl_setopt ( $this->curl, CURLOPT_POSTFIELDS, $post_fields );
curl_setopt ( $this->curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $this->curl, CURLOPT_CONNECTTIMEOUT,20);
curl_setopt ( $this->curl, CURLOPT_TIMEOUT,10);
curl_setopt ( $this->curl, CURLOPT_HEADER, true );
curl_setopt ( $this->curl, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ( $this->curl, CURLOPT_COOKIE, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_COOKIEJAR, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_COOKIEFILE, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
$result = curl_exec ( $this->curl );
this is how it should be:
`Bölüm resimleri, dizi indirme ve altyazı linkine aşağıdan ulaşabilirsiniz.`
this is how it is posted:
`Bölüm resimleri, dizi indirme ve altyazı linkine aşağıdan ulaşabilirsiniz.`
Thanks

From http://php.net/manual/es/function.curl-setopt.php
Try adding:
curl_setopt($this->curl,CURLOPT_HTTPHEADER,array (
"Content-Type: application/x-www-form-urlencoded; charset=utf-8"
));
application/x-www-form-urlencoded suggested by #spencercw

Fixed with
$message = #iconv("UTF-8","Windows-1252//IGNORE",$message);

Related

chatwoot api - create message

I would like to send a message with the chatwoot api.
https://www.chatwoot.com/developers/api/#tag/Messages/operation/create-a-new-message-in-a-conversation
I tried this code:
<?php
$ch = curl_init("https://mydomain.de/api/v1/accounts/1/conversations/100/messages");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(
array(
"content" => "string",
"message_type" => "outgoing",
"private" => true,
"content_type" => "cards"
)
));
curl_setopt($ch, CURLOPT_HTTPHEADER,
array (
'api_access_token: XXX'
),
);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
echo '<pre>';
print_r($response);
echo '</pre>';
?>
Response:
Array
(
[errors] => Array
(
[0] => You must log in or register before you can proceed.
)
)
Any idea?

Saving Facebook Profile picture from URL using cURL not working After March 27 ,2018

Im using the following code:
$json_link = "https://graph.facebook.com/v2.12/me?fields=id,picture.width(300).height(280)&access_token=".$accessToken."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $json_link);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
curl_close($ch);
$obj = json_decode($output);
$xx=$obj->picture->data->url;
$src=imagecreatefromstring(file_get_contents($xx));
$obj returns the following:
stdClass Object (
[id] => idxxxxx
[picture] => stdClass Object (
[data] => stdClass Object (
[height] => 480
[is_silhouette] =>
[url] => lookaside.facebook.com/platform/profilepic/
[width] => 415
)
)
)
But it is failing on:
$xx=$obj->picture->data->url;
$src=imagecreatefromstring(file_get_contents($xx));
I get:
So fails:
imagecreatefromstring(file_get_contents($xx));
It worked fine before March 27, but after graph api update its no longer working.
FB is looking for User-agent information.
$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$request_headers = array();
$request_headers[] = 'User-Agent: '. $User_Agent;
$request_headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$r = curl_exec($ch);
curl_close($ch);
It will solve your problem.
Only put the line: file_put_contents('file.jpg', fopen($obj->picture->data->url, 'r'));

cURL - Navigating multiple forms but can't get passed one page

For a personal project, I'm trying to automate the download of a file from a website that I do not have control/access to. To download the file, user input is required on a total of 6 pages, each with a form submission with the download on the final page.
URL: https://www2.iris.gov.hk/eservices/searchlandregister/search.jsp
I've managed to use cURL to navigate through the first 3 pages. When my cURL code gets the 4th page, it gives me the following error:
"Cannot place order from multiple browser windows, please retry."
This error is generated by the server (not cURL) so I have no idea what it means or how to work around this problem. But if you manually submit the forms on each page, you can progress to the 4th page without error.
Here's the working code. You'll notice the first 3 pages work as expected. However, if you uncomment out the code for the forth page, you'll see that it generates the error message.
I'm stumped! Any help or suggestions would be greatly appreciated.
Thanks!
/**
* Page 1
*
* Sample input data:
*
* Floor: 62
* Flat No: D
* House No: 1
* Street Name: Austin Road West
* Area Code: Kowloon
*
*
*/
$cookie="cookie.txt";
$url = "https://www1.iris.gov.hk/eservices/searchlandregister/search.jsp";
$url2 = "https://www2.iris.gov.hk/eservices/searchLandRegister.do";
$url3 = "https://www2.iris.gov.hk/eservices/searchLandRegister.do";
$url4 = "https://www2.iris.gov.hk/eservices/searchByAddressResult2.do";
$params = array(
'previousOp' => urlencode(''),
'functionId' => urlencode('continue'),
'forward2Caller' => urlencode('searchlandregister'),
'SStreetName' => urlencode('eng'),
'SDevelopmentName' => urlencode('eng'),
'SBlock' => urlencode('*'),
'SFloor' => urlencode('62'),
'SFlat' => urlencode('D'),
'searchBySection' => urlencode('StreetName'),
'SSelectedArea' => urlencode('20'),
'SDevelopmentNameEng' => urlencode(''),
'SDevelopmentNameChn' => urlencode(''),
'SStreetNameEng' => urlencode('AUSTIN ROAD WEST'),
'SStreetNameChn' => urlencode(''),
'hasSpec' => urlencode('0'),
'hasLotSpec' => urlencode('0'),
'specPrn' => urlencode(''),
'haveSDR' => urlencode('Y'),
'initFromRVD' => urlencode('N'),
'SHouseNo1' => urlencode('1'),
'SHouseNo2' => urlencode(''),
'SPrn' => urlencode(''),
'SExtension' => urlencode('false'),
'SLotType' => urlencode(''),
'SLotNo' => urlencode(''),
'SSection1' => urlencode(''),
'SSub1' => urlencode(''),
'SSection2' => urlencode(''),
'SSub2' => urlencode(''),
'SSection3' => urlencode(''),
'SSub3' => urlencode(''),
'SSection4' => urlencode(''),
'SMisc' => urlencode(''),
'recordIndex' => urlencode(''),
'SSBlock' => urlencode('*'),
'SSFloor' => urlencode('62'),
'SSFlat' => urlencode('D'),
'SDBlock' => urlencode('*'),
'SDFloor' => urlencode('*'),
'SDFlat' => urlencode('*'),
'SLBlock' => urlencode('*'),
'SLFloor' => urlencode('*'),
'SLFlat' => urlencode('*'),
'SRegisterName' => urlencode('SDR'),
'SSRegisterName' => urlencode('SDR'),
'SLRegisterName' => urlencode('SDR'),
'SDRegisterName' => urlencode('SDR'),
'xSSBlock' => urlencode('*'),
'xSSFloor' => urlencode('62'),
'xSSFlat' => urlencode('D'),
'xHouseNo1' => urlencode('1'),
'xHouseNo2' => urlencode(''),
'SStreetNameInput' => urlencode('AUSTIN ROAD WEST'),
'xSelectedArea' => urlencode('20'),
'xSSRegisterName' => urlencode('SDR'),
'xSDBlock' => urlencode('*'),
'xSDFloor' => urlencode('*'),
'xSDFlat' => urlencode('*'),
'SDevelopmentNameInput' => urlencode(''),
'xSDRegisterName' => urlencode('SDR'),
'xSLBlock' => urlencode('*'),
'xSLFloor' => urlencode('*'),
'xSLFlat' => urlencode('*'),
'SLotTypeInput' => urlencode(''),
'xLotNo' => urlencode(''),
'xSection1' => urlencode(''),
'xSub1' => urlencode(''),
'xSection2' => urlencode(''),
'xSub2' => urlencode(''),
'xSection3' => urlencode(''),
'xSub3' => urlencode(''),
'xSection4' => urlencode(''),
'xMisc' => urlencode(''),
'xSLRegisterName' => urlencode('SDR'),
'xPrn' => urlencode(''),
// Page 2
'dispLotPRN' => urlencode('C5458863'),
'dispBlock' => urlencode('SO1'),
'BBlock' => urlencode('SO1'),
'hasBlock' => urlencode('1'),
// Page 3
'somePrn[0]' => urlencode('B6479113'),
'someSelected[0]' => urlencode('on'),
'someEnquiry[0]' => urlencode('C'),
'someNature[0]' => urlencode('P'),
'someCopies[0]' => urlencode('1'),
// Page 4
'dispHouseNo' => urlencode('1'),
'dispStreetName' => urlencode('AUSTIN ROAD WEST'),
'hasLot' => urlencode('0'),
'hasFloor' => urlencode('0'),
'street' => urlencode('true'),
'dev' => urlencode('false'),
'lot' => urlencode('false')
);
//url-ify the data for the POST
foreach($params as $key=>$value) {
$string .= $key.'='.$value.'&';
} // end foreach
rtrim($string, '&');
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_POST, false);
$result = curl_exec ($ch);
//echo $result;
// Submit to Page 2
curl_setopt ($ch, CURLOPT_URL, $url2 . '?' . $string);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_POST, false);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec ($ch);
//echo $result;
// Submit to Page 3
$params['previousOp'] = 'BLOCK LIST';
curl_setopt ($ch, CURLOPT_URL, $url3 . '?' . $string);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_POST, false);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec ($ch);
//echo $result;
// Submit to Page 4
/*
$params['previousOp'] = 'EXACT MATCH';
$params['functionId'] = 'doAddAndGoToOrder';
$params['queryResultHash'] = '978962765';
curl_setopt ($ch, CURLOPT_URL, $url4 . '?' . $string);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec ($ch);
*/
curl_close($ch);
echo $result;

cURL on different apache servers

This is part of my code that I use to login on a webpage (links changed):
$curl = curl_init ();
curl_setopt ( $curl, CURLOPT_URL, "loginpage");
curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $curl, CURLOPT_POST, 1 );
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $curl, CURLOPT_POSTFIELDS, "login=login&password=password");
curl_setopt ( $curl, CURLOPT_ENCODING, "" );
curl_setopt ( $curl, CURLOPT_COOKIEFILE, $_SERVER["DOCUMENT_ROOT"].'/web.cookies' );
curl_setopt ( $curl, CURLOPT_COOKIEJAR, $_SERVER["DOCUMENT_ROOT"].'/web.cookies' );
curl_setopt ( $curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)" );
curl_exec ( $curl );
curl_setopt ( $curl, CURLOPT_URL, "homepage");
$profilepage = curl_exec ( $curl );
The thing is, when I run
http://localhost/login.php
I can login and manage my web profile through my script.
The problem is, when I put all my scripts (including this one) on my external paid web server, ie.
http://myserver.com/login.php
I get a response from "loginpage" that "Cookies are not enabled".
What could possibly go wrong?

Connect to https://oauth.live.com/authorize with curl

I'm trying to get windows live connections with php. I Get it with js with w.l. sdk but not with php.
This is my code:
$CLIENT_ID =variable_get('example_contact_grabber_hotmail_client_id',NULL);
$REDIRECT_URL = variable_get('example_contact_grabber_hotmail_url_callback',NULL);
//dpm($REDIRECT_URL);
$url = "https://oauth.live.com/authorize?client_id=$CLIENT_ID&scope=wl.signin&response_type=code&redirect_uri=$REDIRECT_URL";
$url = str_replace( "&", "&", urldecode(trim($url)) );
//dpm($url);
$ch = curl_init($url);
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, TRUE);
$salida = curl_exec($ch);
curl_close($ch);
$todo = split("\n",$salida);
//dpm($todo);
The request give me this message.
Array
(
[0] => <html><head><title>Object moved</title></head><body>
[1] => <h2>Object moved to here.</h2>
[2] => </body></html>
[3] =>
)
If I check $url in Firefox, it goes fine.
Try adding:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//And change
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //true to false
Hope it helps

Categories