I have created php curl to send xml request as under, where it seems that login works. because if there is anything wrong in login detail then it shows error. but if everything is ok then it give response in some garbage value:
Source main code :
$contentlength=strlen($postdata);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL =>$this->url ,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "$postdata",
CURLOPT_HTTPHEADER => array(
"Content-Type: text/xml;charset=utf-8",
"Content-Length: 839",
"Accept: text/xml",
"Accept-Encoding: gzip",
"Authorization : $Authorization",
"User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11",
"Host: $host",
"Connection: close"
),
));
Ouput looks like this :
‹d1oÂ0…÷üŠS–,Û)J›ÈuèÔ"$–²šäH£Æv°MUþ}MÒ€J·»{ŸŸï_}«¾ÐºÖèeÂæ4Ô•©[Ý,““?¤OÉJDÜ¢ëv¸ïLõyåã‡yþ‹ Ç:oñ€6 x
what is wrong ? should i decode anything ? if yes then in which forgat i need to debug. I use securetrading.com site to send xml request.
Simply, you have to remove "Accept-Encoding: gzip" from CURLOPT_HTTPHEADER array.
If still not working, use to decompress output using gzdecode() function
$original_output= gzdecode($curl_output);
For resolve issue of content length you have to count string length of post data, which you have already calculated and stored in $contentlength variable.
"Content-Length:".$contentlength
curl_setopt($curl, CURLOPT_ENCODING , "gzip");
will show properly the result
Related
I was trying to get html response using PHP but always get something like this
screenshot [picture hidden from post due to binary characters]
<?php
$header_request = array (
"accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-encoding: gzip",
"accept-language: id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7",
"cookie: csrf_cookie_name=9316014c9d7860019da66a78edfaf926; _data_pop=115-1_274-1; ci_session=607f0be4e56b8b08ee2398b892f115c9e660192e; _data_cpc=1-2_15-2_190-4",
"referer: https://ptc4btc.com/",
"user-agent: Mozilla/5.0 (Linux; Android 7.0; Moto C Plus) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.56 Mobile Safari/537.36",
"upgrade-insecure-requests: 1"
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "https://ptc4btc.com/dashboard",
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_HTTPHEADER => $header_request,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 2,
));
$exec = curl_exec($ch);
echo($exec);
curl_close($exec);
You said you were willing to accept gzip data with accept-encoding: gzip in the headers. So decode it:
echo gzdecode($exec);
I need to request an api with curl in PHP.
First of all I need to get a token access, and to get it I need to request to this url (with my "client_id") : https://testexellia.bitrix24.fr/oauth/authorize/?response_type=code&client_id=local.5d3c9f79b65439.23207420&redirect_uri=app_URL
It should answer me a new url with the token inside.
It works very well when I request with Chrome, but with curl I get this answer -> https://www.bitrix24.net/oauth/authorize/?user_lang=fr&client_id=b24.5d399107d0f2b9.16511658&redirect_uri=https%3A%2F%2Ftestexellia.bitrix24.fr%2Foauth%2Fauthorize%2F%3Fauth_service_id%3DBitrix24Net%26oauth_proxy_params%3DcmVkaXJlY3RfdXJpPWFwcF9VUkwmY2xpZW50X2lkPWxvY2FsLjVkM2M5Zjc5YjY1NDM5LjIzMjA3NDIwJnJlc3BvbnNlX3R5cGU9Y29kZQ%253D%253D%26sessid%3Db6ded2712b7e66e4b34f05411eb2f4f1&scope=auth,profile&response_type=code&mode=page&state=site_id%3Ds1%26backurl%3D%252Foauth%252Fauthorize%252F%253Fcheck_key%253D7d6109a54034f59c406798d6a9667a9c%2526response_type%253Dcode%2526client_id%253Dlocal.5d3c9f79b65439.23207420%2526redirect_uri%253Dapp_URL%26mode%3Dpage
So the login page.
I know where is the problem, I'm already logged with Chrome (probably with cookies I guess) to the website, and obviously the curl request not.
I guess I need to add some headers informations to my request, like a password and login, but I'm logged to this website with my Google Account.
So is there a way to add Google informations in the curl's header ? I can't find how.
Here is the doc of the API, in case you need.
Edit : Here is the code.
$curl = curl_init();
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://testexellia.bitrix24.fr/oauth/authorize/?response_type=code&client_id=local.5d3c9f79b65439.23207420&redirect_uri=app_URL&sessid=b6ded2712b7e66e4b34f05411eb2f4f1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Authorization: Bearer 08493f5d003eabc0003e9b9e00000001f0f103a29e30c5356d8e8142404add6515e135",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Host: testexellia.bitrix24.fr",
"Postman-Token: 6de6ba41-b9fc-4541-98b0-c2f9d9b1ea7f,b36cd02a-87f8-4fb1-821c-54602a300bc5",
"User-Agent: PostmanRuntime/7.13.0",
"accept-encoding: gzip, deflate",
"cache-control: no-cache",
"cookie: PHPSESSID=fOJKQhrXBNuNmrmIOXXg0YDlIeckdVfm; qmb=.; BITRIX_SM_SALE_UID=0"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
$content = curl_exec($curl);
$redirectedUrl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
curl_close($curl);
echo $redirectedUrl;
Thank you very much.
Benjamin.
I created a POST request on POSTMAN, it work great. But when I export header and form body in PHP cURL code and execute it with PHP CLI, the server does not show me data asked in my form.
Thereafter the PHP cURL exported with POSTMAN :
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://renovation-info-service.gouv.fr/trouvez-un-professionnel",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "all%5Bcols-1%5D%5Bcode_postal%5D=03&all%5Bcols-1%5D%5Bcommune%5D=03&all%5Brayon%5D=5&all%5Bdomaine_travaux%5D=30&op=Localiser%2Bdes%2Bentreprises&form_build_id=form-48ViCRy2vxhZBo452cPiRi22VR7K2mZ5ZUwzR-tlO3U&form_id=front_office__locate_all_entreprise_form",
CURLOPT_HTTPHEADER => array(
"accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"accept-encoding: gzip, deflate",
"accept-language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3",
"cache-control: no-cache",
"connection: keep-alive",
"content-length: 253",
"content-type: application/x-www-form-urlencoded",
"cookie: UUID=3ab96f55-1e99-d7c4-39a9-59345773b4ca; has_js=1; _ga=GA1.3.1779497884.1533297982; _gid=GA1.3.1474901001.1533297982; _gat=1",
"host: renovation-info-service.gouv.fr",
"postman-token: 570c2dd8-8289-dacd-db57-dcb0d107ea29",
"referer: http://renovation-info-service.gouv.fr/trouvez-un-professionnel",
"upgrade-insecure-requests: 1",
"user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Thank you for your help ! And sorry with my bad english ^^'
POSTMAN answer is a html code with data asked in POST request. PHP cURL answer is a html code without data asked in POST request.
I'm reading the past of the question (Request forbidden while accessing github api on node.js program), but still does not solve the problem.
PHP code
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "https://api.github.com/markdown/raw",
CURLOPT_HEADER => [
"Accept: application/vnd.github.v3+json",
"Content-Type: text/plain",
"User-Agent: mfmfneko"
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => "# Hello World!"
]);
$response = curl_exec($ch);
curl_close($ch);
print($response);
Response
HTTP/1.0 403 Forbidden
Cache-Control: no-cache
Connection: close
Content-Type: text/html
Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required). Check https://developer.github.com for other possible causes.
You made a mistake with the installation of headers.
CURLOPT_HEADER TRUE to include the header in the output.
CURLOPT_HTTPHEADER An array of HTTP header fields to set, in the
format array('Content-type: text/plain', 'Content-length: 100')
They should be reported using the CURLOPT_HTTPHEADER
Try this code:
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "https://api.github.com/markdown/raw",
CURLOPT_HTTPHEADER => [
"Accept: application/vnd.github.v3+json",
"Content-Type: text/plain",
"User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 YaBrowser/16.3.0.7146 Yowser/2.5 Safari/537.36"
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => "# Hello World!"
]);
$response = curl_exec($ch);
curl_close($ch);
print($response);
Response:
<h1>
<a id="user-content-hello-world" class="anchor" href="#hello-world" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Hello World!</h1>
More details can be read here
i have a PHPUnit test with an assertion:
$this->assertEquals(json_encode($expected), json_encode($result));
The assertion fails because the strings differ in their encoding: (the diff is actually larger, from a large json object)
Failed asserting that two strings are equal.
--- Expected
+++ Actual
## ##
-'Wir holen Ihre P\u00e4ckchen im Wunschzeitraum ab.'
+'Wir holen Ihre P\u00c3\u00a4ckchen im Wunschzeitraum ab.'
I get the actual string from a curl request, the expected string is defined inside the php file as a string.
I use the following curl code to retrieve the JSON from a server:
$headers = array(
"GET /HTTP/1.1",
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1",
"Content-type: application/json;charset=\"utf-8\"",
"Accept: application/json",
"Accept-Language: en-us,en;q=0.5",
"Accept-Encoding: gzip,deflate",
"Accept-Charset: utf-8;q=0.7,*;q=0.7",
"Keep-Alive: 300",
"Connection: keep-alive");
$ch = curl_init('http://localhost/frontend_test.php/api');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => json_encode($some_data),
CURLOPT_HEADER => 1
));
// Send the request
$response = curl_exec($ch);
list($header, $body) = explode("\r\n\r\n", $response, 2);
$result = json_decode($body);
What may be the cause of this?
okay so the solution is simple in my case: The PDO connection used the wrong charset. (The API retrieved the String in question from the database, which was not encoded properly) Dumb mistake