how to use cURL in facebook graph api request

32,357
    $graph_url= "https://graph.facebook.com/me/photos";
  $postData = "url=" . urlencode($photo_url)
  . "&message=" . urlencode('')
  . "&access_token=" .$access_token;
                
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $graph_url);
        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);
Share:
32,357
Ivo San
Author by

Ivo San

samsung galaxy pocket

Updated on November 16, 2020

Comments

  • Ivo San
    Ivo San over 3 years

    I am getting the information to facebook graph api using file_get_contents()

    but sometimes I am receiving an error.

    I found out that cURL must be used instead of file_get_contents().

    The problem is, I don't know how to use cURL with the URL that I need to pass.

    if I converted

    file_get_contents($graph_url);
    

    into cURL, what will be the code?

    here is the content of $graph_url

    $graph_url= "https://graph.facebook.com/me/photos?"
      . "url=" . urlencode($photo_url)
      . "&message=" . urlencode('')
      . "&method=POST"
      . "&access_token=" .$access_token;
    
    • Martin Bean
      Martin Bean about 11 years
      Read the cURL documentation on how to make requests.
  • Ivo San
    Ivo San about 11 years
    how about if my $graph_url = 'graph.facebook.com'. 'fql?q=SELECT+src_big+FROM+photo+WHERE+object_id='.$photo_id‌​. '&access_token=' . $access_token;
  • Maulik Vora
    Maulik Vora about 11 years
    for fql, file_get_contents is more suitable. I used this function with fql.
  • Maulik Vora
    Maulik Vora about 11 years
    if you want to use curl anyhow, just change CURLOPT_POST to 0 and remove CURLOPT_POSTFIELDS option. also your complete url will be $graph_url = 'graph.facebook.com/fql?q=SELECT+src_big+FROM+photo+WHERE+ob‌​ject_id='.$photo_id. '&access_token=' . $access_token;
  • Maulik Vora
    Maulik Vora over 3 years
    @EM-Creations, your edit is not correct, $graph_url is used for CURLOPT_URL option. $photo_url was used in POST parameter, please undo the change
  • EM-Creations
    EM-Creations over 3 years
    @MaulikVora I understand now after re-reading thanks.
  • hanshenrik
    hanshenrik over 3 years
    would be much prettier using http_build_query() ^^