cURL response not readable - php

I have a problem with my cURL response, when I try to invoke the method of WSDL, I receive this weird text from my browser,
�d�ْ�<�_��[�7�4eS�#���8 �]��Q��A���>�t�,����]�1��%Y���4!l�^ZG��,8��v��������#ZJ�W��
r)0Ek����Q�����"Ᏹ�!�0��0����(�T�$���� Z��փ��UU���g������&�C�f
8�!��5��L�t{����6�?{jY�Q��?���K����3�D2�e �߱Oc����#^P��$�IΠ�4A[;�p�\7�����i5��+�\歖~=����)�����a�L�GJey�F����Ɍ��v]��<��z������N����\z��.�i:�S5��FgkM�hS���|,\�0�E9i=�+ӄ�!^WҶ�7�����0�w���+b�۹��[�;���fE4_ڑ�������u�Q)E��;�;�JL���������Ԩ�1��7[�$D���\�W���ۂU$9���
How can I solve this?
Here's my header
$headers = array(
"Accept-Encoding: gzip, deflate",
"Content-Type: text/xml;charset=\"UTF-8\"",
"SOAPAction: \"http://tempuri.org/"",
"Host: domain.com",
"Content-length: ".strlen($xml_post_string),
"Connection: Keep-Alive"
);
and here's my curl options
curl_setopt($soap_do, CURLOPT_URL, $url);
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 120);
curl_setopt($soap_do, CURLOPT_AUTOREFERER, true); // new
curl_setopt($soap_do, CURLOPT_MAXREDIRS, 10); // new
curl_setopt($soap_do, CURLOPT_FOLLOWLOCATION, true); // new
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($soap_do, CURLOPT_VERBOSE, TRUE);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $headers);

Thank you guys for your comment, I have solve the issue now, shout out to Grokify, I just remove the Accept-Encoding: gzip, deflate and it is now readable.

I had a similar problem. Adding working headers (As of my browser) solved it for me.
Current working code of mine:
$headers = array("User-Agent: ********User Agent********");
$ch = curl_init("******URL*******");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data= curl_exec($ch);
curl_close($ch);

Related

Upload file to Office 365 Onedrive Business account using PHP-Curl

I'm trying to upload a JSON file to a new folder in OneDrive Business AC account using cURL. While uploading, I am getting the following error:
HTTP status code not expected - got - 401
Here is my code:
$uri = "https://graph.microsoft.com/v1.0/me/drive/root/new/sample.json/content?access_token=accesstoken";
function curl_put($uri, $fp) {
$output = "";
try {
$pointer = fopen($fp, 'r+');
$stat = fstat($pointer);
$pointersize = $stat['size'];
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $pointer);
curl_setopt($ch, CURLOPT_INFILESIZE, (int) $pointersize);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
}
}
https://graph.microsoft.com/v1.0/me/drive/root:/foldername/filename:/content
Pass the header as :
$headers = array( 'Content-Type: application/text', "Cache-Control: no-cache", "Pragma: no-cache", "Authorization: bearer ".$token );
and pass the data from the file using file_get_contents.

php curl help on HTTP Status 415

I'm facing issue for getting curl response
test.xml
<?xml version="1.0" encoding="UTF-8"?>
<MMTHotelSearchRequest Offset="0" Rows="500">
<POS>
<Requestor type="B2BAgent" idContext="AFF" id="AFF13856" channel="B2BWeb"/>
<Source iSOCurrency="INR"/>
</POS>
<RequestHotelParams>
<CityCode>DEL,XRO</CityCode>
</RequestHotelParams>
<RequiredFields>hotelInfo,facilitiesInfo,areaInfo,contactInfo,roomsInfo,mediaInfo</RequiredFields>
</MMTHotelSearchRequest>
<?php
$filename = "test.xml";
$handle = fopen($filename, "r");
$xml = fread($handle, filesize($filename));
fclose($handle);
$url = "https://apim-gateway.mmtcloud.com/mmt-htlsearch/1.0/staticsearch/v1.0/hotelData";
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"Content-length: ".strlen($xml),
);
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $url);
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 60);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $xml);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($soap_do);
print_r($result);
?>
I'm getting below error
HTTP Status 415 -
type Status report
message
description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().
Please help me.
The CURLOPT_POSTFIELDS should be an array of key value/pairs. You should create an array, add the xml data to the correct key field and send that data using curl.
I have added an example:
<?php
$filename = "test.xml";
$handle = fopen($filename, "r");
$xml = fread($handle, filesize($filename));
fclose($handle);
$url = "https://apim-gateway.mmtcloud.com/mmt-htlsearch/1.0/staticsearch/v1.0/hotelData";
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"Content-length: ".strlen($xml),
);
$postdata = array("fieldkey" => $xml); //<-------------
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $url);
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 60);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $postdata); //<-----------
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($soap_do);
print_r($result);
?>

convert command curl to php

Hi all! I have curl command (audio file upload):
curl -k -v -H "Expect: " -H "Content-Type:application/octet-stream" --data-binary '#/Path/To/File/test.wav' -X POST 'https://myapi.com?param1=xxx&param2=yyy'
File successfully uploaded and readable. But when I used php script:
$filename = 'test.wav';
$file = '#/Path/To/File/test.wav';
$post_url = "https://someapi.com?param1=xxx&param2=yyy";
$post_str = array("$filename" => $file);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/octet-stream'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$http_body = curl_exec($ch);
var_dump($http_body);
File successfully uploaded and test.wav is not valid (with some error). What am I doing wrong in the script?
I suspect the issue is these lines:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/octet-stream'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
The second call will remove the "Content-Type" Header. Try consolidating these:
$headers = array('Content-Type:application/octet-stream','Expect: ');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$filename = 'test.wav';
$file = '/Path/To/File/test.wav';
$post_url = "https://someapi.com?param1=xxx&param2=yyy";
$post_str = file_get_contents($file);
$headers = array('Content-Type:application/octet-stream', 'Expect: ');
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_VERBOSE,true);
curl_setopt($ch, CURLOPT_STDERR, fopen("/Path/to/header.txt", "w+"));
$http_body = curl_exec($ch);
It's work perfect. I solved the problem myself. File upload to the server binary. Thank you all!

soap curl request works but response doesnt

I am supposed to receive a variable called "D" after server receives the string I sent. How do I get it to receive the server responses message?
$soapme = curl_init();
curl_setopt($soapme, CURLOPT_URL, $url );
curl_setopt($soapme, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($soapme, CURLOPT_TIMEOUT, 10);
curl_setopt($soapme, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soapme, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soapme, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soapme, CURLOPT_POST, true );
curl_setopt($soapme, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($soapme, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($post_string) ));
curl_setopt($soapme, CURLOPT_USERPWD, $user . ":" . $password);
curl_setopt($soapme, curlOPT_VERBOSE, 1);
curl_exec($soapme);
I notice I get a 1 as a result when I do not use ReturnTransfers. What does this 1 mean?
$result = curl_exec($soapme);
echo $result;

trying to grab data from a page after post via curl

i am trying to grab data from here : http://mediaforest.biz/mobile/nowplaying.aspx
in the page you select a station and post it then you get new page with data. but i cant grab it, i get the same page again.
i used this code:
<?php
header ('Content-type: text/html; charset=utf-8');
$url = "http://mediaforest.biz/mobile/nowplaying.aspx";
$referer = "";
// headers
$header[] = "Host: ".parse_url($url, PHP_URL_HOST);
$header[] = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; he; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3";
$header[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$header[] = "Accept-Language: he,en-us;q=0.7,en;q=0.3";
$header[] = "Accept-Encoding: gzip,deflate";
$header[] = "Accept-Charset: windows-1255,utf-8;q=0.7,*;q=0.7";
$header[] = "Keep-Alive: 115";
$header[] = "Connection: keep-alive";
$cookie="cookie.txt";
$fp=fopen($cookie,"w+");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_REFERER,$referer);
curl_setopt($ch, CURLOPT_TIMEOUT, 900);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$content=curl_exec($ch);
echo $content;
if(stristr($content,"__EVENTTARGET")){
$array1=explode('__EVENTTARGET" value="',$content);
$content1=$array1[1];
$array2=explode('">
<input type="hidden" name="__EVENTARGUMENT"',$content1);
$content2=$array2[0];
$EVENTTARGET=urlencode($content2);
}
if(stristr($content,"__EVENTARGUMENT")){
$array1=explode('__EVENTARGUMENT" value="',$content);
$content1=$array1[1];
$array2=explode('">
<script language',$content1);
$content2=$array2[0];
$EVENTARGUMENT=urlencode($content2);
}
if(stristr($content,"formNowPlaying")){
$array1=explode('method="post" action="',$content);
$content1=$array1[1];
$array2=explode('">
<input type="hidden" name="__EVENTTARGET"',$content1);
$content2=$array2[0];
$nexturl=$content2;
}
//echo $EVENTTARGET." ".$EVENTARGUMENT." ".$nexturl;
$url = "http://mediaforest.biz/mobile/".$nexturl;
$fields = "EVENTTARGET=".$EVENTTARGET."&__EVENTARGUMENT=".$EVENTARGUMENT."&MyChannels=0&ViewChannel_Button=Show";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_REFERER,$referer);
curl_setopt($ch, CURLOPT_TIMEOUT, 900);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$content_stage2=curl_exec($ch);
echo $content_stage2;
?>
If you're getting data from the first request, try closing afterwards (for each request).
$content=curl_exec($ch);
curl_close($ch);
echo $content;
and
$content_stage2=curl_exec($ch);
curl_close($ch);
echo $content_stage2;
I don't have much experience with cURL, and I didn't look into it, but at first glance it appears that this:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
needs to be this:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Could be wrong though.

Categories