Solved

php curl, getting bad characters

Posted on 2010-11-16
8
932 Views
Last Modified: 2012-05-10
I am trying to gather data from a french website
using curl

I am getting
Complément téléobjectif


but the site says
Complément téléobjectif


0
Comment
Question by:rgb192
  • 4
  • 3
8 Comments
 
LVL 7

Expert Comment

by:printnix63
ID: 34151072
You will have to URL-Encode them, these are double byte characters and URL/curl will only get plain ASCII. or if it is a POST request, request data binary.

From the curl Manual:
-d/--data <data>
(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a
browser does when a user has filled in an HTML form and presses the submit button. This will
cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.
Compare to -F/--form.
-d/--data is the same as --data-ascii. To post data purely binary, you should instead use the --databinary
option. To URL-encode the value of a form field you may use --data-urlencode.
If any of these options is used more than once on the same command line, the data pieces specified
will be merged together with a separating &-symbol. Thus, using ’-d name=daniel -d skill=lousy’
would generate a post chunk that looks like ’name=daniel&skill=lousy’.
If you start the data with the letter @, the rest should be a file name to read the data from, or - if
you want curl to read the data from stdin. The contents of the file must already be URL-encoded.
Multiple files can also be specified. Posting data from a file named ’foobar’ would thus be done
with --data @foobar.
--data-binary <data>
(HTTP) This posts data exactly as specified with no extra processing whatsoever.
If you start the data with the letter @, the rest should be a filename. Data is posted in a similar
manner as --data-ascii does, except that newlines are preserved and conversions are never done.
If this option is used several times, the ones following the first will append data as described in
-d/--data.
--data-urlencode <data>
(HTTP) This posts data, similar to the other --data options with the exception that this performs
URL-encoding. (Added in 7.18.0)

Maybe you'll have to check on the php side for the implementation.
The letter encoding is definitly not what curl did expect.
0
 

Author Comment

by:rgb192
ID: 34151455

so what should I change this line to
header("Content-type: text/plain");
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34155220
What is the URL of the web site?  I may be able to show you how to retrieve the letters correctly.  I do not think that the accented characters are double-byte.
0
 

Author Comment

by:rgb192
ID: 34157061
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34158462
CURL retrieves the data correctly, and I find things like Périphériques et Stockage in the text.  

Are you telling the browser that this is UTF-8?  Because Amazon is using this character set:
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

<?php // RAY_temp_rgb192.php

error_reporting(E_ALL);



// A FUNCTION TO RUN A CURL-GET CLIENT CALL TO A FOREIGN SERVER

function my_curl($url, $timeout=2, $error_report=FALSE)

{

    $curl = curl_init();



    // HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE

    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,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: "; // BROWSERS USUALLY LEAVE BLANK



    // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php

    curl_setopt( $curl, CURLOPT_URL,            $url  );

    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'  );

    curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );

    curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );

    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );

    curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );

    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );

    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );

    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );



    // RUN THE CURL REQUEST AND GET THE RESULTS

    $htm = curl_exec($curl);



    // ON FAILURE HANDLE ERROR MESSAGE

    if ($htm === FALSE)

    {

        if ($error_report)

        {

            $err = curl_errno($curl);

            $inf = curl_getinfo($curl);

            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";

            var_dump($inf);

        }

        curl_close($curl);

        return FALSE;

    }



    // ON SUCCESS RETURN XML / HTML STRING

    curl_close($curl);

    return $htm;

}









// USAGE EXAMPLE - PUT YOUR FAVORITE URL HERE

$url = "http://www.amazon.fr/gp/product/B000V9D5LG";

$htm = my_curl($url, 5, TRUE);

if (!$htm) die("NO $url");





// SHOW WHAT WE GOT

echo "<pre>";

$htm = preg_replace('/ +/', ' ', $htm);

$htm = preg_replace('/\n+/', PHP_EOL, $htm);

echo htmlentities($htm);

Open in new window

0
 

Author Comment

by:rgb192
ID: 34176047
so if I change this line in my script from
header("Content-type: text/plain");

to

header("Content-type: charset=iso-8859-1");


it may work
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34176627
It's easy enough to try.  I never send the Content-type header - I let my server figure it out for me.
0
 

Author Closing Comment

by:rgb192
ID: 34220314
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now