curl php alwaysing getting error 403 - php

hello im trying to get a page using curl but always getting error 403. Is there any way to get this web page?
My source code is like this
<?php
set_time_limit(0);
error_reporting(0);
$LOGINURL = "https://sso.orange.fr/espace-client/desimlockage";
$ch = curl_init();
$headers[] = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
$headers[] = "Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4";
$headers[] = "Connection: Keep-Alive";
$headers[] = "Host:iz.orange.fr";
$headers[] = "Referer:http://www.sosh.fr/espace-client-accueil";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'User-Agent: curl/7.39.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $LOGINURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$content = curl_exec($ch);
echo $content;
?>

comment curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); and try once. hope it'll help.

Related

Uploading Textfiles to GCP-Storage with php changes the content of the file

I would like to upload a textfile to Google Cloud Storage.
I use this function in php:
function upload_object_to_Google_bucket($bucket,$accesstoken,$proxy,$objectname,$objectpath)
{
#Build the CUrl: incarnate.github.io/curl-to-php
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, "https://storage.googleapis.com/upload/storage/v1/b/".$bucket."/o?uploadType=media&name=".$objectname);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
# curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
$file=$objectpath.$objectname;
$item=make_curl_file($file);
print_r($item);
$file = $objectpath.$objectname;
$post = array(
$item
);
# curl_setopt($ch, CURLOPT_UPLOAD, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
# curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
# curl_setopt($ch, CURLOPT_NOBODY, true);
$headers = array();
$headers[] = 'Authorization: Bearer '.$accesstoken;
$headers[] = 'Content-Type: '.object_to_array($item)["mime"];
$headers[] = 'Content-Length: '.filesize($file);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
print_r($response);
if (curl_errno($ch)){
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
}
It's all fine, but the textfile has become new content in the Bucket.
--------------------------9ad622dfd933f230
Content-Disposition: form-data; name="0"; filename="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xml"
Content-Type: text/plain
These lines are added to the file. Does anyone has an idea whats the problem and how I can avoid this behaviour?

How to do a PUT request with curl and Authorization?

This is what I am trying
$url = "https://aon-test.yui.com/api/".$this->_token."/".$action;
$headers = array(
'Content-Type:application/xml' ,
'Authorization: Basic '. base64_encode("s!YIYIYIYIUYIY:X")
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 30000);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($xml));
$content = curl_exec($ch);
curl_close($ch);
I am getting 500 - Internal server error.
What mistake I am doing here?

POST file Curl Commandline to PHP Curl

We are trying to convert use the following curl cmdline which is working fine
curl --request POST --data-binary #"/home/project/enrol/my.wav" --header "Content-Type:audio/wav" --header "VsitEmail: 200037test2#test.com" --header "VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282" --header "VsitDeveloperId: 200037" https://siv.voiceprintportal.com/sivservice/api/enrollments
to PHP libcurl which is constantly giving internal error.
Please suggest where i am wrong .
Code
$ch = curl_init();
$path="/home/project/enrol/my.wav";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: audio/wav","VsitEmail: 200037test2#test.com","VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282","VsitDeveloperId: 200037"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt( $ch , CURLOPT_VERBOSE , 0 );
curl_setopt($ch, CURLOPT_POSTFIELDS,array('file' => '#'.$path));
$result = curl_exec($ch);
//close connection
curl_close($ch);
print_r($result);
Update 1
$localfile='/home/project/enrol/my.wav';
$url = 'https://siv.voiceprintportal.com/sivservice/api/enrollments';
$ch = curl_init();
$fields = array('file' => '#' .$localfile);
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
//curl_setopt($resource, CURLOPT_HTTPHEADER, $header);
curl_setopt($resource, CURLOPT_HTTPHEADER, array("Content-Type:
audio/wav","VsitEmail: 200037test2#test.com","VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282","VsitDeveloperId: 200037"));
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_BINARYTRANSFER, true);
curl_setopt($resource, CURLOPT_POSTFIELDS, $fields);
//$result = json_decode(curl_exec($resource));
$result=curl_exec($resource);
curl_close($resource);
echo "The result is";
print_r($result);
Check the following code
$url = 'http://google.com';
$header = array('Content-Type: multipart/form-data');
$fields = array('file' => '#' . $_FILES['file']['tmp_name'][0]);
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_HTTPHEADER, $header);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($resource);
curl_close($resource);
It got resolved by passing the raw data file instead of an array. Correct code is
$localfile='#/home/project/enrol/my.wav';
$url = 'https://siv.voiceprintportal.com/sivservice/api/enrollments';
$data = file_get_contents('/home/project/enrol/my.wav');
$headers = array();
$headers[] = 'X-Requested-With: JSONHttpRequest';
$headers[] = 'Content-Type: audio/wav';
$headers[] = 'VsitEmail: 200037test2#test.com';
$headers[] = 'VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282';
$headers[] = 'VsitDeveloperId: 200037';
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_HTTPHEADER,$headers);
curl_setopt($resource, CURLOPT_POST,1);
curl_setopt($resource, CURLOPT_BINARYTRANSFER, true);
curl_setopt($resource, CURLOPT_POSTFIELDS,$data);
$result = json_decode(curl_exec($resource));
curl_close($resource);
echo "The result is";
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!

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